├── .gitignore ├── .gitmodules ├── AUTHORS ├── CODEOWNERS ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE ├── README.md ├── WORKSPACE └── research ├── deeplab ├── README.md ├── __init__.py ├── common.py ├── common_test.py ├── core │ ├── __init__.py │ ├── dense_prediction_cell.py │ ├── dense_prediction_cell_branch5_top1_cityscapes.json │ ├── dense_prediction_cell_test.py │ ├── feature_extractor.py │ ├── nas_cell.py │ ├── nas_genotypes.py │ ├── nas_network.py │ ├── nas_network_test.py │ ├── preprocess_utils.py │ ├── preprocess_utils_test.py │ ├── resnet_v1_beta.py │ ├── resnet_v1_beta_test.py │ ├── utils.py │ ├── utils_test.py │ ├── xception.py │ └── xception_test.py ├── count_flops.ipynb ├── datasets │ ├── __init__.py │ ├── build_ade20k_data.py │ ├── build_cityscapes_data.py │ ├── build_data.py │ ├── build_voc2012_data.py │ ├── convert_cityscapes.sh │ ├── data_generator.py │ ├── data_generator_test.py │ ├── download_and_convert_ade20k.sh │ ├── download_and_convert_voc2012.sh │ └── remove_gt_colormap.py ├── deeplab_demo.ipynb ├── deprecated │ ├── __init__.py │ └── segmentation_dataset.py ├── docs │ └── chart.png ├── eval.py ├── eval_nus_mobilenetv2.sh ├── evaluation │ ├── README.md │ ├── __init__.py │ ├── base_metric.py │ ├── eval_coco_format.py │ ├── eval_coco_format_test.py │ ├── g3doc │ │ └── img │ │ │ ├── equation_pc.png │ │ │ └── equation_pq.png │ ├── panoptic_quality.py │ ├── panoptic_quality_test.py │ ├── parsing_covering.py │ ├── parsing_covering_test.py │ ├── streaming_metrics.py │ ├── streaming_metrics_test.py │ ├── test_utils.py │ ├── test_utils_test.py │ └── testdata │ │ ├── README.md │ │ ├── bird_gt.png │ │ ├── bird_pred_class.png │ │ ├── bird_pred_instance.png │ │ ├── cat_gt.png │ │ ├── cat_pred_class.png │ │ ├── cat_pred_instance.png │ │ ├── coco_gt.json │ │ ├── coco_gt │ │ ├── bird.png │ │ ├── cat.png │ │ ├── congress.png │ │ └── team.png │ │ ├── coco_pred.json │ │ ├── coco_pred │ │ ├── bird.png │ │ ├── cat.png │ │ ├── congress.png │ │ └── team.png │ │ ├── team_gt_instance.png │ │ ├── team_pred_class.png │ │ └── team_pred_instance.png ├── export_model.py ├── g3doc │ ├── ade20k.md │ ├── cityscapes.md │ ├── export_model.md │ ├── faq.md │ ├── img │ │ ├── image1.jpg │ │ ├── image2.jpg │ │ ├── image3.jpg │ │ ├── image_info.txt │ │ ├── vis1.png │ │ ├── vis2.png │ │ └── vis3.png │ ├── installation.md │ ├── model_zoo.md │ ├── pascal.md │ └── quantize.md ├── input_preprocess.py ├── local_test.sh ├── local_test_mobilenetv2.sh ├── model.py ├── model_test.py ├── nus.py ├── nus_common.sh ├── test_mobilenetv2_with_nus.sh ├── test_nus_mobilenetv2.sh ├── testing │ ├── info.md │ └── pascal_voc_seg │ │ └── val-00000-of-00001.tfrecord ├── train.py ├── utils │ ├── __init__.py │ ├── get_dataset_colormap.py │ ├── get_dataset_colormap_test.py │ ├── nus │ │ ├── CMakeLists.txt │ │ ├── UseCython.cmake │ │ ├── __init__.py │ │ ├── build.sh │ │ ├── closest_point.cpp │ │ ├── closest_point.h │ │ ├── closest_point_cython.pyx │ │ ├── interpolation.py │ │ ├── sample.py │ │ └── utils.py │ ├── save_annotation.py │ └── train_utils.py └── vis.py └── slim ├── BUILD ├── README.md ├── WORKSPACE ├── __init__.py ├── datasets ├── __init__.py ├── build_imagenet_data.py ├── build_visualwakewords_data.py ├── build_visualwakewords_data_lib.py ├── cifar10.py ├── dataset_factory.py ├── dataset_utils.py ├── download_and_convert_cifar10.py ├── download_and_convert_flowers.py ├── download_and_convert_imagenet.sh ├── download_and_convert_mnist.py ├── download_imagenet.sh ├── download_mscoco.sh ├── flowers.py ├── imagenet.py ├── imagenet_2012_validation_synset_labels.txt ├── imagenet_lsvrc_2015_synsets.txt ├── imagenet_metadata.txt ├── mnist.py ├── preprocess_imagenet_validation_data.py ├── process_bounding_boxes.py └── visualwakewords.py ├── deployment ├── __init__.py ├── model_deploy.py └── model_deploy_test.py ├── download_and_convert_data.py ├── eval_image_classifier.py ├── export_inference_graph.py ├── export_inference_graph_test.py ├── nets ├── __init__.py ├── alexnet.py ├── alexnet_test.py ├── cifarnet.py ├── cyclegan.py ├── cyclegan_test.py ├── dcgan.py ├── dcgan_test.py ├── i3d.py ├── i3d_test.py ├── i3d_utils.py ├── inception.py ├── inception_resnet_v2.py ├── inception_resnet_v2_test.py ├── inception_utils.py ├── inception_v1.py ├── inception_v1_test.py ├── inception_v2.py ├── inception_v2_test.py ├── inception_v3.py ├── inception_v3_test.py ├── inception_v4.py ├── inception_v4_test.py ├── lenet.py ├── mobilenet │ ├── README.md │ ├── __init__.py │ ├── conv_blocks.py │ ├── madds_top1_accuracy.png │ ├── mnet_v1_vs_v2_pixel1_latency.png │ ├── mobilenet.py │ ├── mobilenet_example.ipynb │ ├── mobilenet_v2.py │ └── mobilenet_v2_test.py ├── mobilenet_v1.md ├── mobilenet_v1.png ├── mobilenet_v1.py ├── mobilenet_v1_eval.py ├── mobilenet_v1_test.py ├── mobilenet_v1_train.py ├── nasnet │ ├── README.md │ ├── __init__.py │ ├── nasnet.py │ ├── nasnet_test.py │ ├── nasnet_utils.py │ ├── nasnet_utils_test.py │ ├── pnasnet.py │ └── pnasnet_test.py ├── nets_factory.py ├── nets_factory_test.py ├── overfeat.py ├── overfeat_test.py ├── pix2pix.py ├── pix2pix_test.py ├── resnet_utils.py ├── resnet_v1.py ├── resnet_v1_test.py ├── resnet_v2.py ├── resnet_v2_test.py ├── s3dg.py ├── s3dg_test.py ├── vgg.py └── vgg_test.py ├── preprocessing ├── __init__.py ├── cifarnet_preprocessing.py ├── inception_preprocessing.py ├── lenet_preprocessing.py ├── preprocessing_factory.py └── vgg_preprocessing.py ├── scripts ├── export_mobilenet.sh ├── finetune_inception_resnet_v2_on_flowers.sh ├── finetune_inception_v1_on_flowers.sh ├── finetune_inception_v3_on_flowers.sh ├── finetune_resnet_v1_50_on_flowers.sh ├── train_cifarnet_on_cifar10.sh └── train_lenet_on_mnist.sh ├── setup.py ├── slim_walkthrough.ipynb └── train_image_classifier.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | 91 | # PyCharm 92 | .idea/ 93 | 94 | # For mac 95 | .DS_Store 96 | 97 | # Training data for word embedding tutorial 98 | tutorials/embedding/text8 99 | tutorials/embedding/questions-words.txt 100 | 101 | samples/outreach/blogs/segmentation_blogpost/carvana-image-masking-challenge/ 102 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/.gitmodules -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # This is the official list of authors for copyright purposes. 2 | # This file is distinct from the CONTRIBUTORS files. 3 | # See the latter for an explanation. 4 | 5 | # Names should be added to this file as: 6 | # Name or Organization 7 | # The email address is not required for organizations. 8 | 9 | Google Inc. 10 | David Dao 11 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @tensorflow/tf-garden-team 2 | /official/ @tfboyd @guptapriya @rachellj218 @saberkun 3 | /official/bert @saberkun @hongjunChoi @rachellj218 4 | /research/adversarial_crypto/ @dave-andersen 5 | /research/adversarial_logit_pairing/ @AlexeyKurakin 6 | /research/adversarial_text/ @rsepassi @a-dai 7 | /research/adv_imagenet_models/ @AlexeyKurakin 8 | /research/attention_ocr/ @alexgorban 9 | /research/audioset/ @plakal @dpwe 10 | /research/autoaugment/* @barretzoph 11 | /research/autoencoders/ @snurkabill 12 | /research/brain_coder/ @danabo 13 | /research/cognitive_mapping_and_planning/ @s-gupta 14 | /research/compression/ @nmjohn 15 | /research/cvt_text/ @clarkkev @lmthang 16 | /research/deep_contextual_bandits/ @rikel 17 | /research/deeplab/ @aquariusjay @yknzhu @gpapan 18 | /research/delf/ @andrefaraujo 19 | /research/differential_privacy/ @ilyamironov @ananthr 20 | /research/domain_adaptation/ @bousmalis @dmrd 21 | /research/efficient-hrl/ @ofirnachum 22 | /research/gan/ @joel-shor 23 | /research/global_objectives/ @mackeya-google 24 | /research/im2txt/ @cshallue 25 | /research/inception/ @shlens @vincentvanhoucke 26 | /research/keypointnet/ @mnorouzi 27 | /research/learned_optimizer/ @olganw @nirum 28 | /research/learning_to_remember_rare_events/ @lukaszkaiser @ofirnachum 29 | /research/learning_unsupervised_learning/ @lukemetz @nirum 30 | /research/lexnet_nc/ @vered1986 @waterson 31 | /research/lfads/ @jazcollins @susillo 32 | /research/lm_1b/ @oriolvinyals @panyx0718 33 | /research/lm_commonsense/ @thtrieu 34 | /research/lstm_object_detection/ @dreamdragon @masonliuw @yinxiaoli @yongzhe2160 35 | /research/marco/ @vincentvanhoucke 36 | /research/maskgan/ @a-dai 37 | /research/morph_net/ @gariel-google 38 | /research/namignizer/ @knathanieltucker 39 | /research/neural_gpu/ @lukaszkaiser 40 | /research/neural_programmer/ @arvind2505 41 | /research/next_frame_prediction/ @panyx0718 42 | /research/object_detection/ @jch1 @tombstone @derekjchow @jesu9 @dreamdragon @pkulzc 43 | /research/pcl_rl/ @ofirnachum 44 | /research/ptn/ @xcyan @arkanath @hellojas @honglaklee 45 | /research/real_nvp/ @laurent-dinh 46 | /research/rebar/ @gjtucker 47 | /research/resnet/ @panyx0718 48 | /research/seq2species/ @apbusia @depristo 49 | /research/skip_thoughts/ @cshallue 50 | /research/slim/ @sguada @nathansilberman 51 | /research/steve/ @buckman-google 52 | /research/street/ @theraysmith 53 | /research/struct2depth/ @aneliaangelova 54 | /research/swivel/ @waterson 55 | /research/syntaxnet/ @calberti @andorardo @bogatyy @markomernick 56 | /research/tcn/ @coreylynch @sermanet 57 | /research/tensorrt/ @karmel 58 | /research/textsum/ @panyx0718 @peterjliu 59 | /research/transformer/ @daviddao 60 | /research/vid2depth/ @rezama 61 | /research/video_prediction/ @cbfinn 62 | /research/fivo/ @dieterichlawson 63 | /samples/ @MarkDaoust @lamberta 64 | /samples/languages/java/ @asimshankar 65 | /tutorials/embedding/ @zffchen78 @a-dai 66 | /tutorials/image/ @sherrym @shlens 67 | /tutorials/image/cifar10_estimator/ @tfboyd @protoget 68 | /tutorials/rnn/ @lukaszkaiser @ebrevdo 69 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing guidelines 2 | 3 | If you have created a model and would like to publish it here, please send us a 4 | pull request. For those just getting started with pull requests, GitHub has a 5 | [howto](https://help.github.com/articles/using-pull-requests/). 6 | 7 | The code for any model in this repository is licensed under the Apache License 8 | 2.0. 9 | 10 | In order to accept our code, we have to make sure that we can publish your code: 11 | You have to sign a Contributor License Agreement (CLA). 12 | 13 | ### Contributor License Agreements 14 | 15 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 16 | 17 | * 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). 18 | * 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). 19 | 20 | Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests. 21 | 22 | ***NOTE***: Only original source code from you and other people that have signed the CLA can be accepted into the repository. 23 | 24 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please go to Stack Overflow for help and support: 2 | 3 | http://stackoverflow.com/questions/tagged/tensorflow 4 | 5 | Also, please understand that many of the models included in this repository are experimental and research-style code. If you open a GitHub issue, here is our policy: 6 | 7 | 1. It must be a bug, a feature request, or a significant problem with documentation (for small docs fixes please send a PR instead). 8 | 2. The form below must be filled out. 9 | 10 | **Here's why we have that policy**: TensorFlow developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow. 11 | 12 | ------------------------ 13 | 14 | ### System information 15 | - **What is the top-level directory of the model you are using**: 16 | - **Have I written custom code (as opposed to using a stock example script provided in TensorFlow)**: 17 | - **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**: 18 | - **TensorFlow installed from (source or binary)**: 19 | - **TensorFlow version (use command below)**: 20 | - **Bazel version (if compiling from source)**: 21 | - **CUDA/cuDNN version**: 22 | - **GPU model and memory**: 23 | - **Exact command to reproduce**: 24 | 25 | You can collect some of this information using our environment capture script: 26 | 27 | https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh 28 | 29 | You can obtain the TensorFlow version with 30 | 31 | `python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"` 32 | 33 | ### Describe the problem 34 | Describe the problem clearly here. Be sure to convey here why it's a bug in TensorFlow or a feature request. 35 | 36 | ### Source code / logs 37 | Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem. 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Efficient Segmentation: Learning Downsampling Near Semantic Boundaries 2 | 3 | This is an implementation independently developed by Dmitrii Marin based on open materials 4 | ([arXiv:1907.07156](https://arxiv.org/abs/1907.07156)). If you find the code useful for your 5 | research, please consider citing our latest publication: 6 | 7 | * Dmitrii Marin, Zijian He, Peter Vajda, Priyam Chatterjee, Sam Tsai, Fei Yang, Yuri Boykov. 8 | **Efficient Segmentation: Learning Downsampling Near Semantic Boundaries.** *In International Conference on Computer Vision (ICCV)*, 2019 9 | 10 | ``` 11 | @InProceedings{marin2019efficient, 12 | author = {Dmitrii Marin, Zijian He, Peter Vajda, Priyam Chatterjee, Sam Tsai, Fei Yang, Yuri Boykov}, 13 | title = {Efficient Segmentation: Learning Downsampling Near Semantic Boundaries}, 14 | booktitle = {IEEE International Conference on Computer Vision (ICCV)}, 15 | year = {2019}, 16 | } 17 | ``` 18 | 19 | ### MobileNetV2 results 20 | 21 | ![MobileNetV2 results](research/deeplab/docs/chart.png) 22 | 23 | ## Training 24 | 25 | The very first step is to download the CityScapes dataset and convert it to tensorflow records format. See [here](research/deeplab/g3doc/cityscapes.md) 26 | 27 | ### Training Sampling Near Semantic Boundaries 28 | 29 | 30 | #### - Prerequisite 31 | 32 | Compile the module that produces sampling locations from ground truth boundaries (Eq.2 in the paper). 33 | ``` 34 | cd research/deeplab/utils/nus 35 | sh build.sh 36 | ``` 37 | You will need CMake, [Ceres Solver](http://ceres-solver.org/), Cython and a C++ compiler. 38 | 39 | #### - Training 40 | 41 | To train the auxiliary network predicting adaptive sampling location run 42 | ``` 43 | bash -x test_nus_mobilenetv2.sh 44 | ``` 45 | 46 | To evaluate MSE on the validation set run 47 | ``` 48 | bash -x eval_nus_mobilenetv2.sh 49 | ``` 50 | 51 | ### Training Segmentation 52 | 53 | To train a MobileNetV2 model with our adaptive downsampling (to resolution 192x192) on cityscapes dataset run 54 | ``` 55 | CUDA_VISIBLE_DEVICES=0 SAMPLING_SIZE=192 bash -x test_mobilenetv2_with_nus.sh 56 | ``` 57 | To start an infinite loop evaluating checkpoints run 58 | ``` 59 | CUDA_VISIBLE_DEVICES= MODE=EVAL SAMPLING_SIZE=192 bash -x test_mobilenetv2_with_nus.sh 60 | ``` 61 | 62 | #### Baseline 63 | ``` 64 | CUDA_VISIBLE_DEVICES=1 SAMPLING_SIZE=192 UNIFORM=1 bash -x test_mobilenetv2_with_nus.sh 65 | CUDA_VISIBLE_DEVICES= MODE=EVAL SAMPLING_SIZE=192 UNIFORM=1 bash -x test_mobilenetv2_with_nus.sh 66 | ``` 67 | 68 | ## Changes with respect to the approach in the [paper](https://arxiv.org/abs/1907.07156v1) 69 | 70 | * The base model is Deeplabv3 with [MobileNetv2](https://arxiv.org/abs/1801.04381) backbone. The original paper uses 71 | [PSP-Net](https://arxiv.org/abs/1612.01105), [Deeplabv3+ with Xception](https://github.com/tensorflow/models/tree/master/research/deeplab) and 72 | [U-Net](https://arxiv.org/abs/1505.04597). 73 | 74 | * The auxiliary network is MobileNetv2 with reduced number of channels 75 | and reduced input resolution. The original paper uses two stacked U-Net models. 76 | 77 | ## Acknowledgements 78 | 79 | The implementation is based of the DeepLab v3 implementation hosted at 80 | [Tensorflow's models repository](https://github.com/tensorflow/models). 81 | For compactness we have removed parts unused in our implementation. 82 | 83 | ## License 84 | 85 | [Apache License 2.0](LICENSE) 86 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/WORKSPACE -------------------------------------------------------------------------------- /research/deeplab/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/__init__.py -------------------------------------------------------------------------------- /research/deeplab/common_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for common.py.""" 17 | import copy 18 | 19 | import tensorflow as tf 20 | 21 | from deeplab import common 22 | 23 | 24 | class CommonTest(tf.test.TestCase): 25 | 26 | def testOutputsToNumClasses(self): 27 | num_classes = 21 28 | model_options = common.ModelOptions( 29 | outputs_to_num_classes={common.OUTPUT_TYPE: num_classes}) 30 | self.assertEqual(model_options.outputs_to_num_classes[common.OUTPUT_TYPE], 31 | num_classes) 32 | 33 | def testDeepcopy(self): 34 | num_classes = 21 35 | model_options = common.ModelOptions( 36 | outputs_to_num_classes={common.OUTPUT_TYPE: num_classes}) 37 | model_options_new = copy.deepcopy(model_options) 38 | self.assertEqual((model_options_new. 39 | outputs_to_num_classes[common.OUTPUT_TYPE]), 40 | num_classes) 41 | 42 | num_classes_new = 22 43 | model_options_new.outputs_to_num_classes[common.OUTPUT_TYPE] = ( 44 | num_classes_new) 45 | self.assertEqual(model_options.outputs_to_num_classes[common.OUTPUT_TYPE], 46 | num_classes) 47 | self.assertEqual((model_options_new. 48 | outputs_to_num_classes[common.OUTPUT_TYPE]), 49 | num_classes_new) 50 | 51 | if __name__ == '__main__': 52 | tf.test.main() 53 | -------------------------------------------------------------------------------- /research/deeplab/core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/core/__init__.py -------------------------------------------------------------------------------- /research/deeplab/core/dense_prediction_cell_branch5_top1_cityscapes.json: -------------------------------------------------------------------------------- 1 | [{"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}] -------------------------------------------------------------------------------- /research/deeplab/core/nas_genotypes.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Genotypes used by NAS.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | from deeplab.core import nas_cell 23 | 24 | 25 | class PNASCell(nas_cell.NASBaseCell): 26 | """Configuration and construction of the PNASNet-5 Cell.""" 27 | 28 | def __init__(self, num_conv_filters, drop_path_keep_prob, total_num_cells, 29 | total_training_steps): 30 | # Name of operations: op_kernel-size_num-layers. 31 | operations = [ 32 | 'separable_5x5_2', 'max_pool_3x3', 'separable_7x7_2', 'max_pool_3x3', 33 | 'separable_5x5_2', 'separable_3x3_2', 'separable_3x3_2', 'max_pool_3x3', 34 | 'separable_3x3_2', 'none' 35 | ] 36 | used_hiddenstates = [1, 1, 0, 0, 0, 0, 0] 37 | hiddenstate_indices = [1, 1, 0, 0, 0, 0, 4, 0, 1, 0] 38 | 39 | super(PNASCell, self).__init__( 40 | num_conv_filters, operations, used_hiddenstates, hiddenstate_indices, 41 | drop_path_keep_prob, total_num_cells, total_training_steps) 42 | -------------------------------------------------------------------------------- /research/deeplab/core/nas_network_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for resnet_v1_beta module.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import numpy as np 23 | import tensorflow as tf 24 | 25 | from deeplab.core import nas_genotypes 26 | from deeplab.core import nas_network 27 | 28 | arg_scope = tf.contrib.framework.arg_scope 29 | slim = tf.contrib.slim 30 | 31 | 32 | def create_test_input(batch, height, width, channels): 33 | """Creates test input tensor.""" 34 | if None in [batch, height, width, channels]: 35 | return tf.placeholder(tf.float32, (batch, height, width, channels)) 36 | else: 37 | return tf.to_float( 38 | np.tile( 39 | np.reshape( 40 | np.reshape(np.arange(height), [height, 1]) + 41 | np.reshape(np.arange(width), [1, width]), 42 | [1, height, width, 1]), 43 | [batch, 1, 1, channels])) 44 | 45 | 46 | class NASNetworkTest(tf.test.TestCase): 47 | """Tests with complete small NAS networks.""" 48 | 49 | def _pnasnet(self, 50 | images, 51 | backbone, 52 | num_classes, 53 | is_training=True, 54 | output_stride=16, 55 | final_endpoint=None): 56 | """Build PNASNet model backbone.""" 57 | hparams = tf.contrib.training.HParams( 58 | filter_scaling_rate=2.0, 59 | num_conv_filters=10, 60 | drop_path_keep_prob=1.0, 61 | total_training_steps=200000, 62 | ) 63 | if not is_training: 64 | hparams.set_hparam('drop_path_keep_prob', 1.0) 65 | 66 | cell = nas_genotypes.PNASCell(hparams.num_conv_filters, 67 | hparams.drop_path_keep_prob, 68 | len(backbone), 69 | hparams.total_training_steps) 70 | with arg_scope([slim.dropout, slim.batch_norm], is_training=is_training): 71 | return nas_network._build_nas_base( 72 | images, 73 | cell=cell, 74 | backbone=backbone, 75 | num_classes=num_classes, 76 | hparams=hparams, 77 | reuse=tf.AUTO_REUSE, 78 | scope='pnasnet_small', 79 | final_endpoint=final_endpoint) 80 | 81 | def testFullyConvolutionalEndpointShapes(self): 82 | num_classes = 10 83 | backbone = [0, 0, 0, 1, 2, 1, 2, 2, 3, 3, 2, 1] 84 | inputs = create_test_input(None, 321, 321, 3) 85 | with slim.arg_scope(nas_network.nas_arg_scope()): 86 | _, end_points = self._pnasnet(inputs, backbone, num_classes) 87 | endpoint_to_shape = { 88 | 'Stem': [None, 81, 81, 128], 89 | 'Cell_0': [None, 81, 81, 50], 90 | 'Cell_1': [None, 81, 81, 50], 91 | 'Cell_2': [None, 81, 81, 50], 92 | 'Cell_3': [None, 41, 41, 100], 93 | 'Cell_4': [None, 21, 21, 200], 94 | 'Cell_5': [None, 41, 41, 100], 95 | 'Cell_6': [None, 21, 21, 200], 96 | 'Cell_7': [None, 21, 21, 200], 97 | 'Cell_8': [None, 11, 11, 400], 98 | 'Cell_9': [None, 11, 11, 400], 99 | 'Cell_10': [None, 21, 21, 200], 100 | 'Cell_11': [None, 41, 41, 100] 101 | } 102 | for endpoint, shape in endpoint_to_shape.items(): 103 | self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) 104 | 105 | 106 | if __name__ == '__main__': 107 | tf.test.main() 108 | -------------------------------------------------------------------------------- /research/deeplab/core/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """This script contains utility functions.""" 17 | import tensorflow as tf 18 | 19 | slim = tf.contrib.slim 20 | 21 | 22 | def resize_bilinear(images, size, output_dtype=tf.float32): 23 | """Returns resized images as output_type. 24 | 25 | Args: 26 | images: A tensor of size [batch, height_in, width_in, channels]. 27 | size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size 28 | for the images. 29 | output_dtype: The destination type. 30 | Returns: 31 | A tensor of size [batch, height_out, width_out, channels] as a dtype of 32 | output_dtype. 33 | """ 34 | images = tf.image.resize_bilinear(images, size, align_corners=True) 35 | return tf.cast(images, dtype=output_dtype) 36 | 37 | 38 | def scale_dimension(dim, scale): 39 | """Scales the input dimension. 40 | 41 | Args: 42 | dim: Input dimension (a scalar or a scalar Tensor). 43 | scale: The amount of scaling applied to the input. 44 | 45 | Returns: 46 | Scaled dimension. 47 | """ 48 | if isinstance(dim, tf.Tensor): 49 | return tf.cast((tf.to_float(dim) - 1.0) * scale + 1.0, dtype=tf.int32) 50 | else: 51 | return int((float(dim) - 1.0) * scale + 1.0) 52 | 53 | 54 | def split_separable_conv2d(inputs, 55 | filters, 56 | kernel_size=3, 57 | rate=1, 58 | weight_decay=0.00004, 59 | depthwise_weights_initializer_stddev=0.33, 60 | pointwise_weights_initializer_stddev=0.06, 61 | scope=None): 62 | """Splits a separable conv2d into depthwise and pointwise conv2d. 63 | 64 | This operation differs from `tf.layers.separable_conv2d` as this operation 65 | applies activation function between depthwise and pointwise conv2d. 66 | 67 | Args: 68 | inputs: Input tensor with shape [batch, height, width, channels]. 69 | filters: Number of filters in the 1x1 pointwise convolution. 70 | kernel_size: A list of length 2: [kernel_height, kernel_width] of 71 | of the filters. Can be an int if both values are the same. 72 | rate: Atrous convolution rate for the depthwise convolution. 73 | weight_decay: The weight decay to use for regularizing the model. 74 | depthwise_weights_initializer_stddev: The standard deviation of the 75 | truncated normal weight initializer for depthwise convolution. 76 | pointwise_weights_initializer_stddev: The standard deviation of the 77 | truncated normal weight initializer for pointwise convolution. 78 | scope: Optional scope for the operation. 79 | 80 | Returns: 81 | Computed features after split separable conv2d. 82 | """ 83 | outputs = slim.separable_conv2d( 84 | inputs, 85 | None, 86 | kernel_size=kernel_size, 87 | depth_multiplier=1, 88 | rate=rate, 89 | weights_initializer=tf.truncated_normal_initializer( 90 | stddev=depthwise_weights_initializer_stddev), 91 | weights_regularizer=None, 92 | scope=scope + '_depthwise') 93 | return slim.conv2d( 94 | outputs, 95 | filters, 96 | 1, 97 | weights_initializer=tf.truncated_normal_initializer( 98 | stddev=pointwise_weights_initializer_stddev), 99 | weights_regularizer=slim.l2_regularizer(weight_decay), 100 | scope=scope + '_pointwise') 101 | -------------------------------------------------------------------------------- /research/deeplab/core/utils_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for utils.py.""" 16 | 17 | import tensorflow as tf 18 | 19 | from deeplab.core import utils 20 | 21 | 22 | class UtilsTest(tf.test.TestCase): 23 | 24 | def testScaleDimensionOutput(self): 25 | self.assertEqual(161, utils.scale_dimension(321, 0.5)) 26 | self.assertEqual(193, utils.scale_dimension(321, 0.6)) 27 | self.assertEqual(241, utils.scale_dimension(321, 0.75)) 28 | 29 | 30 | if __name__ == '__main__': 31 | tf.test.main() 32 | -------------------------------------------------------------------------------- /research/deeplab/datasets/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/datasets/__init__.py -------------------------------------------------------------------------------- /research/deeplab/datasets/build_ade20k_data.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Converts ADE20K data to TFRecord file format with Example protos.""" 17 | 18 | import math 19 | import os 20 | import random 21 | import sys 22 | import build_data 23 | import tensorflow as tf 24 | 25 | FLAGS = tf.app.flags.FLAGS 26 | 27 | tf.app.flags.DEFINE_string( 28 | 'train_image_folder', 29 | './ADE20K/ADEChallengeData2016/images/training', 30 | 'Folder containing trainng images') 31 | tf.app.flags.DEFINE_string( 32 | 'train_image_label_folder', 33 | './ADE20K/ADEChallengeData2016/annotations/training', 34 | 'Folder containing annotations for trainng images') 35 | 36 | tf.app.flags.DEFINE_string( 37 | 'val_image_folder', 38 | './ADE20K/ADEChallengeData2016/images/validation', 39 | 'Folder containing validation images') 40 | 41 | tf.app.flags.DEFINE_string( 42 | 'val_image_label_folder', 43 | './ADE20K/ADEChallengeData2016/annotations/validation', 44 | 'Folder containing annotations for validation') 45 | 46 | tf.app.flags.DEFINE_string( 47 | 'output_dir', './ADE20K/tfrecord', 48 | 'Path to save converted tfrecord of Tensorflow example') 49 | 50 | _NUM_SHARDS = 4 51 | 52 | 53 | def _convert_dataset(dataset_split, dataset_dir, dataset_label_dir): 54 | """Converts the ADE20k dataset into into tfrecord format. 55 | 56 | Args: 57 | dataset_split: Dataset split (e.g., train, val). 58 | dataset_dir: Dir in which the dataset locates. 59 | dataset_label_dir: Dir in which the annotations locates. 60 | 61 | Raises: 62 | RuntimeError: If loaded image and label have different shape. 63 | """ 64 | 65 | img_names = tf.gfile.Glob(os.path.join(dataset_dir, '*.jpg')) 66 | random.shuffle(img_names) 67 | seg_names = [] 68 | for f in img_names: 69 | # get the filename without the extension 70 | basename = os.path.basename(f).split('.')[0] 71 | # cover its corresponding *_seg.png 72 | seg = os.path.join(dataset_label_dir, basename+'.png') 73 | seg_names.append(seg) 74 | 75 | num_images = len(img_names) 76 | num_per_shard = int(math.ceil(num_images / float(_NUM_SHARDS))) 77 | 78 | image_reader = build_data.ImageReader('jpeg', channels=3) 79 | label_reader = build_data.ImageReader('png', channels=1) 80 | 81 | for shard_id in range(_NUM_SHARDS): 82 | output_filename = os.path.join( 83 | FLAGS.output_dir, 84 | '%s-%05d-of-%05d.tfrecord' % (dataset_split, shard_id, _NUM_SHARDS)) 85 | with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer: 86 | start_idx = shard_id * num_per_shard 87 | end_idx = min((shard_id + 1) * num_per_shard, num_images) 88 | for i in range(start_idx, end_idx): 89 | sys.stdout.write('\r>> Converting image %d/%d shard %d' % ( 90 | i + 1, num_images, shard_id)) 91 | sys.stdout.flush() 92 | # Read the image. 93 | image_filename = img_names[i] 94 | image_data = tf.gfile.FastGFile(image_filename, 'rb').read() 95 | height, width = image_reader.read_image_dims(image_data) 96 | # Read the semantic segmentation annotation. 97 | seg_filename = seg_names[i] 98 | seg_data = tf.gfile.FastGFile(seg_filename, 'rb').read() 99 | seg_height, seg_width = label_reader.read_image_dims(seg_data) 100 | if height != seg_height or width != seg_width: 101 | raise RuntimeError('Shape mismatched between image and label.') 102 | # Convert to tf example. 103 | example = build_data.image_seg_to_tfexample( 104 | image_data, img_names[i], height, width, seg_data) 105 | tfrecord_writer.write(example.SerializeToString()) 106 | sys.stdout.write('\n') 107 | sys.stdout.flush() 108 | 109 | 110 | def main(unused_argv): 111 | tf.gfile.MakeDirs(FLAGS.output_dir) 112 | _convert_dataset( 113 | 'train', FLAGS.train_image_folder, FLAGS.train_image_label_folder) 114 | _convert_dataset('val', FLAGS.val_image_folder, FLAGS.val_image_label_folder) 115 | 116 | 117 | if __name__ == '__main__': 118 | tf.app.run() 119 | -------------------------------------------------------------------------------- /research/deeplab/datasets/convert_cityscapes.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # Script to preprocess the Cityscapes dataset. Note (1) the users should 18 | # register the Cityscapes dataset website at 19 | # https://www.cityscapes-dataset.com/downloads/ to download the dataset, 20 | # and (2) the users should download the utility scripts provided by 21 | # Cityscapes at https://github.com/mcordts/cityscapesScripts. 22 | # 23 | # Usage: 24 | # bash ./preprocess_cityscapes.sh 25 | # 26 | # The folder structure is assumed to be: 27 | # + datasets 28 | # - build_cityscapes_data.py 29 | # - convert_cityscapes.sh 30 | # + cityscapes 31 | # + cityscapesscripts (downloaded scripts) 32 | # + gtFine 33 | # + leftImg8bit 34 | # 35 | 36 | # Exit immediately if a command exits with a non-zero status. 37 | set -e 38 | 39 | CURRENT_DIR=$(pwd) 40 | WORK_DIR="." 41 | 42 | # Root path for Cityscapes dataset. 43 | CITYSCAPES_ROOT="${WORK_DIR}/cityscapes" 44 | 45 | # Create training labels. 46 | python "${CITYSCAPES_ROOT}/cityscapesscripts/preparation/createTrainIdLabelImgs.py" 47 | 48 | # Build TFRecords of the dataset. 49 | # First, create output directory for storing TFRecords. 50 | OUTPUT_DIR="${CITYSCAPES_ROOT}/tfrecord" 51 | mkdir -p "${OUTPUT_DIR}" 52 | 53 | BUILD_SCRIPT="${CURRENT_DIR}/build_cityscapes_data.py" 54 | 55 | echo "Converting Cityscapes dataset..." 56 | python "${BUILD_SCRIPT}" \ 57 | --cityscapes_root="${CITYSCAPES_ROOT}" \ 58 | --output_dir="${OUTPUT_DIR}" \ 59 | -------------------------------------------------------------------------------- /research/deeplab/datasets/download_and_convert_ade20k.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # Script to download and preprocess the ADE20K dataset. 18 | # 19 | # Usage: 20 | # bash ./download_and_convert_ade20k.sh 21 | # 22 | # The folder structure is assumed to be: 23 | # + datasets 24 | # - build_data.py 25 | # - build_ade20k_data.py 26 | # - download_and_convert_ade20k.sh 27 | # + ADE20K 28 | # + tfrecord 29 | # + ADEChallengeData2016 30 | # + annotations 31 | # + training 32 | # + validation 33 | # + images 34 | # + training 35 | # + validation 36 | 37 | # Exit immediately if a command exits with a non-zero status. 38 | set -e 39 | 40 | CURRENT_DIR=$(pwd) 41 | WORK_DIR="./ADE20K" 42 | mkdir -p "${WORK_DIR}" 43 | cd "${WORK_DIR}" 44 | 45 | # Helper function to download and unpack ADE20K dataset. 46 | download_and_uncompress() { 47 | local BASE_URL=${1} 48 | local FILENAME=${2} 49 | 50 | if [ ! -f "${FILENAME}" ]; then 51 | echo "Downloading ${FILENAME} to ${WORK_DIR}" 52 | wget -nd -c "${BASE_URL}/${FILENAME}" 53 | fi 54 | echo "Uncompressing ${FILENAME}" 55 | unzip "${FILENAME}" 56 | } 57 | 58 | # Download the images. 59 | BASE_URL="http://data.csail.mit.edu/places/ADEchallenge" 60 | FILENAME="ADEChallengeData2016.zip" 61 | 62 | download_and_uncompress "${BASE_URL}" "${FILENAME}" 63 | 64 | cd "${CURRENT_DIR}" 65 | 66 | # Root path for ADE20K dataset. 67 | ADE20K_ROOT="${WORK_DIR}/ADEChallengeData2016" 68 | 69 | # Build TFRecords of the dataset. 70 | # First, create output directory for storing TFRecords. 71 | OUTPUT_DIR="${WORK_DIR}/tfrecord" 72 | mkdir -p "${OUTPUT_DIR}" 73 | 74 | echo "Converting ADE20K dataset..." 75 | python ./build_ade20k_data.py \ 76 | --train_image_folder="${ADE20K_ROOT}/images/training/" \ 77 | --train_image_label_folder="${ADE20K_ROOT}/annotations/training/" \ 78 | --val_image_folder="${ADE20K_ROOT}/images/validation/" \ 79 | --val_image_label_folder="${ADE20K_ROOT}/annotations/validation/" \ 80 | --output_dir="${OUTPUT_DIR}" 81 | -------------------------------------------------------------------------------- /research/deeplab/datasets/download_and_convert_voc2012.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # Script to download and preprocess the PASCAL VOC 2012 dataset. 18 | # 19 | # Usage: 20 | # bash ./download_and_convert_voc2012.sh 21 | # 22 | # The folder structure is assumed to be: 23 | # + datasets 24 | # - build_data.py 25 | # - build_voc2012_data.py 26 | # - download_and_convert_voc2012.sh 27 | # - remove_gt_colormap.py 28 | # + pascal_voc_seg 29 | # + VOCdevkit 30 | # + VOC2012 31 | # + JPEGImages 32 | # + SegmentationClass 33 | # 34 | 35 | # Exit immediately if a command exits with a non-zero status. 36 | set -e 37 | 38 | CURRENT_DIR=$(pwd) 39 | WORK_DIR="./pascal_voc_seg" 40 | mkdir -p "${WORK_DIR}" 41 | cd "${WORK_DIR}" 42 | 43 | # Helper function to download and unpack VOC 2012 dataset. 44 | download_and_uncompress() { 45 | local BASE_URL=${1} 46 | local FILENAME=${2} 47 | 48 | if [ ! -f "${FILENAME}" ]; then 49 | echo "Downloading ${FILENAME} to ${WORK_DIR}" 50 | wget -nd -c "${BASE_URL}/${FILENAME}" 51 | fi 52 | echo "Uncompressing ${FILENAME}" 53 | tar -xf "${FILENAME}" 54 | } 55 | 56 | # Download the images. 57 | BASE_URL="http://host.robots.ox.ac.uk/pascal/VOC/voc2012/" 58 | FILENAME="VOCtrainval_11-May-2012.tar" 59 | 60 | download_and_uncompress "${BASE_URL}" "${FILENAME}" 61 | 62 | cd "${CURRENT_DIR}" 63 | 64 | # Root path for PASCAL VOC 2012 dataset. 65 | PASCAL_ROOT="${WORK_DIR}/VOCdevkit/VOC2012" 66 | 67 | # Remove the colormap in the ground truth annotations. 68 | SEG_FOLDER="${PASCAL_ROOT}/SegmentationClass" 69 | SEMANTIC_SEG_FOLDER="${PASCAL_ROOT}/SegmentationClassRaw" 70 | 71 | echo "Removing the color map in ground truth annotations..." 72 | python ./remove_gt_colormap.py \ 73 | --original_gt_folder="${SEG_FOLDER}" \ 74 | --output_dir="${SEMANTIC_SEG_FOLDER}" 75 | 76 | # Build TFRecords of the dataset. 77 | # First, create output directory for storing TFRecords. 78 | OUTPUT_DIR="${WORK_DIR}/tfrecord" 79 | mkdir -p "${OUTPUT_DIR}" 80 | 81 | IMAGE_FOLDER="${PASCAL_ROOT}/JPEGImages" 82 | LIST_FOLDER="${PASCAL_ROOT}/ImageSets/Segmentation" 83 | 84 | echo "Converting PASCAL VOC 2012 dataset..." 85 | python ./build_voc2012_data.py \ 86 | --image_folder="${IMAGE_FOLDER}" \ 87 | --semantic_segmentation_folder="${SEMANTIC_SEG_FOLDER}" \ 88 | --list_folder="${LIST_FOLDER}" \ 89 | --image_format="jpg" \ 90 | --output_dir="${OUTPUT_DIR}" 91 | -------------------------------------------------------------------------------- /research/deeplab/datasets/remove_gt_colormap.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Removes the color map from segmentation annotations. 17 | 18 | Removes the color map from the ground truth segmentation annotations and save 19 | the results to output_dir. 20 | """ 21 | import glob 22 | import os.path 23 | import numpy as np 24 | 25 | from PIL import Image 26 | 27 | import tensorflow as tf 28 | 29 | FLAGS = tf.app.flags.FLAGS 30 | 31 | tf.app.flags.DEFINE_string('original_gt_folder', 32 | './VOCdevkit/VOC2012/SegmentationClass', 33 | 'Original ground truth annotations.') 34 | 35 | tf.app.flags.DEFINE_string('segmentation_format', 'png', 'Segmentation format.') 36 | 37 | tf.app.flags.DEFINE_string('output_dir', 38 | './VOCdevkit/VOC2012/SegmentationClassRaw', 39 | 'folder to save modified ground truth annotations.') 40 | 41 | 42 | def _remove_colormap(filename): 43 | """Removes the color map from the annotation. 44 | 45 | Args: 46 | filename: Ground truth annotation filename. 47 | 48 | Returns: 49 | Annotation without color map. 50 | """ 51 | return np.array(Image.open(filename)) 52 | 53 | 54 | def _save_annotation(annotation, filename): 55 | """Saves the annotation as png file. 56 | 57 | Args: 58 | annotation: Segmentation annotation. 59 | filename: Output filename. 60 | """ 61 | pil_image = Image.fromarray(annotation.astype(dtype=np.uint8)) 62 | with tf.gfile.Open(filename, mode='w') as f: 63 | pil_image.save(f, 'PNG') 64 | 65 | 66 | def main(unused_argv): 67 | # Create the output directory if not exists. 68 | if not tf.gfile.IsDirectory(FLAGS.output_dir): 69 | tf.gfile.MakeDirs(FLAGS.output_dir) 70 | 71 | annotations = glob.glob(os.path.join(FLAGS.original_gt_folder, 72 | '*.' + FLAGS.segmentation_format)) 73 | for annotation in annotations: 74 | raw_annotation = _remove_colormap(annotation) 75 | filename = os.path.basename(annotation)[:-4] 76 | _save_annotation(raw_annotation, 77 | os.path.join( 78 | FLAGS.output_dir, 79 | filename + '.' + FLAGS.segmentation_format)) 80 | 81 | 82 | if __name__ == '__main__': 83 | tf.app.run() 84 | -------------------------------------------------------------------------------- /research/deeplab/deprecated/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/deprecated/__init__.py -------------------------------------------------------------------------------- /research/deeplab/docs/chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/docs/chart.png -------------------------------------------------------------------------------- /research/deeplab/eval_nus_mobilenetv2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Modified by Dmitrii Marin, https://github.com/dmitrii-marin 3 | # 4 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ============================================================================== 18 | # 19 | # This script is used to run local test on PASCAL VOC 2012 using MobileNet-v2. 20 | # Users could also modify from this script for their use case. 21 | # 22 | # Usage: 23 | # # From the tensorflow/models/research/deeplab directory. 24 | # sh ./local_test_mobilenetv2.sh 25 | # 26 | # 27 | 28 | 29 | source "nus_common.sh" 30 | 31 | PASCAL_FOLDER="cityscapes" 32 | EXP_FOLDER="exp/nus${AUX_INPUT_SIZE}_128_mobilenetv2_lr${LEARNING_RATE}_mt${DEPTH_MULTIPLIER}" 33 | INIT_FOLDER="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/init_models" 34 | TRAIN_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/train" 35 | EVAL_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/eval" 36 | VIS_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/vis" 37 | EXPORT_DIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/export" 38 | mkdir -p "${INIT_FOLDER}" 39 | mkdir -p "${TRAIN_LOGDIR}" 40 | mkdir -p "${EVAL_LOGDIR}" 41 | mkdir -p "${VIS_LOGDIR}" 42 | mkdir -p "${EXPORT_DIR}" 43 | 44 | PASCAL_DATASET="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/tfrecord" 45 | 46 | 47 | # Run evaluation. This performs eval over the full val split (1449 images) and 48 | # will take a while. 49 | # Using the provided checkpoint, one should expect mIOU=75.34%. 50 | python "${WORK_DIR}"/eval.py \ 51 | --logtostderr \ 52 | --dataset="cityscapes" \ 53 | --eval_split="val" \ 54 | --model_variant="mobilenet_v2" \ 55 | --eval_type="nus" \ 56 | --nus_net_input_size=${AUX_INPUT_SIZE} \ 57 | --nus_net_stride=${STRIDE} \ 58 | --nus_sampling_size=${SAMPLING_SIZE} \ 59 | --nus_depth_multiplier=${DEPTH_MULTIPLIER} \ 60 | --max_resize_value=${AUX_INPUT_SIZE} \ 61 | --min_resize_value=${AUX_INPUT_SIZE} \ 62 | --nus_target_classes="11,12,13,14,15,16,17,18"\ 63 | --eval_crop_size="1024,1024" \ 64 | --checkpoint_dir="${TRAIN_LOGDIR}" \ 65 | --eval_logdir="${EVAL_LOGDIR}" \ 66 | --dataset_dir="${PASCAL_DATASET}" \ 67 | --max_number_of_evaluations=-1 68 | -------------------------------------------------------------------------------- /research/deeplab/evaluation/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/__init__.py -------------------------------------------------------------------------------- /research/deeplab/evaluation/g3doc/img/equation_pc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/g3doc/img/equation_pc.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/g3doc/img/equation_pq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/g3doc/img/equation_pq.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/test_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Utility functions to set up unit tests on Panoptic Segmentation code.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import os 22 | 23 | 24 | 25 | from absl import flags 26 | import numpy as np 27 | import scipy.misc 28 | import six 29 | 30 | FLAGS = flags.FLAGS 31 | 32 | _TEST_DIR = 'deeplab/evaluation/testdata' 33 | 34 | 35 | def read_test_image(testdata_path, *args, **kwargs): 36 | """Loads a test image. 37 | 38 | Args: 39 | testdata_path: Image path relative to panoptic_segmentation/testdata as a 40 | string. 41 | *args: Additional positional arguments passed to `imread`. 42 | **kwargs: Additional keyword arguments passed to `imread`. 43 | 44 | Returns: 45 | The image, as a numpy array. 46 | """ 47 | image_path = os.path.join(_TEST_DIR, testdata_path) 48 | return scipy.misc.imread(image_path, *args, **kwargs) 49 | 50 | 51 | def read_segmentation_with_rgb_color_map(image_testdata_path, 52 | rgb_to_semantic_label, 53 | output_dtype=None): 54 | """Reads a test segmentation as an image and a map from colors to labels. 55 | 56 | Args: 57 | image_testdata_path: Image path relative to panoptic_segmentation/testdata 58 | as a string. 59 | rgb_to_semantic_label: Mapping from RGB colors to integer labels as a 60 | dictionary. 61 | output_dtype: Type of the output labels. If None, defaults to the type of 62 | the provided color map. 63 | 64 | Returns: 65 | A 2D numpy array of labels. 66 | 67 | Raises: 68 | ValueError: On an incomplete `rgb_to_semantic_label`. 69 | """ 70 | rgb_image = read_test_image(image_testdata_path, mode='RGB') 71 | if len(rgb_image.shape) != 3 or rgb_image.shape[2] != 3: 72 | raise AssertionError( 73 | 'Expected RGB image, actual shape is %s' % rgb_image.sape) 74 | 75 | num_pixels = rgb_image.shape[0] * rgb_image.shape[1] 76 | unique_colors = np.unique(np.reshape(rgb_image, [num_pixels, 3]), axis=0) 77 | if not set(map(tuple, unique_colors)).issubset( 78 | six.viewkeys(rgb_to_semantic_label)): 79 | raise ValueError('RGB image has colors not in color map.') 80 | 81 | output_dtype = output_dtype or type( 82 | next(six.itervalues(rgb_to_semantic_label))) 83 | output_labels = np.empty(rgb_image.shape[:2], dtype=output_dtype) 84 | for rgb_color, int_label in six.iteritems(rgb_to_semantic_label): 85 | color_array = np.array(rgb_color, ndmin=3) 86 | output_labels[np.all(rgb_image == color_array, axis=2)] = int_label 87 | return output_labels 88 | 89 | 90 | def panoptic_segmentation_with_class_map(instance_testdata_path, 91 | instance_label_to_semantic_label): 92 | """Reads in a panoptic segmentation with an instance map and a map to classes. 93 | 94 | Args: 95 | instance_testdata_path: Path to a grayscale instance map, given as a string 96 | and relative to panoptic_segmentation/testdata. 97 | instance_label_to_semantic_label: A map from instance labels to class 98 | labels. 99 | 100 | Returns: 101 | A tuple `(instance_labels, class_labels)` of numpy arrays. 102 | 103 | Raises: 104 | ValueError: On a mismatched set of instances in 105 | the 106 | `instance_label_to_semantic_label`. 107 | """ 108 | instance_labels = read_test_image(instance_testdata_path, mode='L') 109 | if set(np.unique(instance_labels)) != set( 110 | six.iterkeys(instance_label_to_semantic_label)): 111 | raise ValueError('Provided class map does not match present instance ids.') 112 | 113 | class_labels = np.empty_like(instance_labels) 114 | for instance_id, class_id in six.iteritems(instance_label_to_semantic_label): 115 | class_labels[instance_labels == instance_id] = class_id 116 | 117 | return instance_labels, class_labels 118 | -------------------------------------------------------------------------------- /research/deeplab/evaluation/test_utils_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for test_utils.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | 22 | 23 | from absl.testing import absltest 24 | import numpy as np 25 | 26 | from deeplab.evaluation import test_utils 27 | 28 | 29 | class TestUtilsTest(absltest.TestCase): 30 | 31 | def test_read_test_image(self): 32 | image_array = test_utils.read_test_image('team_pred_class.png') 33 | self.assertSequenceEqual(image_array.shape, (231, 345, 4)) 34 | 35 | def test_reads_segmentation_with_color_map(self): 36 | rgb_to_semantic_label = {(0, 0, 0): 0, (0, 0, 255): 1, (255, 0, 0): 23} 37 | labels = test_utils.read_segmentation_with_rgb_color_map( 38 | 'team_pred_class.png', rgb_to_semantic_label) 39 | 40 | input_image = test_utils.read_test_image('team_pred_class.png') 41 | np.testing.assert_array_equal( 42 | labels == 0, 43 | np.logical_and(input_image[:, :, 0] == 0, input_image[:, :, 2] == 0)) 44 | np.testing.assert_array_equal(labels == 1, input_image[:, :, 2] == 255) 45 | np.testing.assert_array_equal(labels == 23, input_image[:, :, 0] == 255) 46 | 47 | def test_reads_gt_segmentation(self): 48 | instance_label_to_semantic_label = { 49 | 0: 0, 50 | 47: 1, 51 | 97: 1, 52 | 133: 1, 53 | 150: 1, 54 | 174: 1, 55 | 198: 23, 56 | 215: 1, 57 | 244: 1, 58 | 255: 1, 59 | } 60 | instances, classes = test_utils.panoptic_segmentation_with_class_map( 61 | 'team_gt_instance.png', instance_label_to_semantic_label) 62 | 63 | expected_label_shape = (231, 345) 64 | self.assertSequenceEqual(instances.shape, expected_label_shape) 65 | self.assertSequenceEqual(classes.shape, expected_label_shape) 66 | np.testing.assert_array_equal(instances == 0, classes == 0) 67 | np.testing.assert_array_equal(instances == 198, classes == 23) 68 | np.testing.assert_array_equal( 69 | np.logical_and(instances != 0, instances != 198), classes == 1) 70 | 71 | 72 | if __name__ == '__main__': 73 | absltest.main() 74 | -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/README.md: -------------------------------------------------------------------------------- 1 | # Segmentation Evalaution Test Data 2 | 3 | ## Source Images 4 | 5 | * [team_input.png](team_input.png) \ 6 | Source: 7 | https://ai.googleblog.com/2018/03/semantic-image-segmentation-with.html 8 | * [cat_input.jpg](cat_input.jpg) \ 9 | Source: https://www.flickr.com/photos/magdalena_b/4995858743 10 | * [bird_input.jpg](bird_input.jpg) \ 11 | Source: https://www.flickr.com/photos/chivinskia/40619099560 12 | * [congress_input.jpg](congress_input.jpg) \ 13 | Source: 14 | https://cao.house.gov/sites/cao.house.gov/files/documents/SAR-Jan-Jun-2016.pdf 15 | -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/bird_gt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/bird_gt.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/bird_pred_class.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/bird_pred_class.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/bird_pred_instance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/bird_pred_instance.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/cat_gt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/cat_gt.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/cat_pred_class.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/cat_pred_class.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/cat_pred_instance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/cat_pred_instance.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_gt.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": { 3 | "description": "Test COCO-format dataset", 4 | "url": "https://github.com/tensorflow/models/tree/master/research/deeplab", 5 | "version": "1.0", 6 | "year": 2019 7 | }, 8 | "images": [ 9 | { 10 | "id": 1, 11 | "file_name": "bird.jpg", 12 | "height": 159, 13 | "width": 240, 14 | "flickr_url": "https://www.flickr.com/photos/chivinskia/40619099560" 15 | }, 16 | { 17 | "id": 2, 18 | "file_name": "cat.jpg", 19 | "height": 330, 20 | "width": 317, 21 | "flickr_url": "https://www.flickr.com/photos/magdalena_b/4995858743" 22 | }, 23 | { 24 | "id": 3, 25 | "file_name": "team.jpg", 26 | "height": 231, 27 | "width": 345 28 | }, 29 | { 30 | "id": 4, 31 | "file_name": "congress.jpg", 32 | "height": 267, 33 | "width": 525 34 | } 35 | ], 36 | "annotations": [ 37 | { 38 | "image_id": 1, 39 | "file_name": "bird.png", 40 | "segments_info": [ 41 | { 42 | "id": 255, 43 | "area": 2913, 44 | "category_id": 4, 45 | "iscrowd": 0 46 | }, 47 | { 48 | "id": 2586368, 49 | "area": 1580, 50 | "category_id": 3, 51 | "iscrowd": 0 52 | }, 53 | { 54 | "id": 16770360, 55 | "area": 33667, 56 | "category_id": 5, 57 | "iscrowd": 0 58 | } 59 | ] 60 | }, 61 | { 62 | "image_id": 2, 63 | "file_name": "cat.png", 64 | "segments_info": [ 65 | { 66 | "id": 16711691, 67 | "area": 34454, 68 | "category_id": 6, 69 | "iscrowd": 0 70 | } 71 | ] 72 | }, 73 | { 74 | "image_id": 3, 75 | "file_name": "team.png", 76 | "segments_info": [ 77 | { 78 | "id": 129, 79 | "area": 5443, 80 | "category_id": 1, 81 | "iscrowd": 0 82 | }, 83 | { 84 | "id": 255, 85 | "area": 3574, 86 | "category_id": 2, 87 | "iscrowd": 0 88 | }, 89 | { 90 | "id": 47615, 91 | "area": 11483, 92 | "category_id": 1, 93 | "iscrowd": 0 94 | }, 95 | { 96 | "id": 65532, 97 | "area": 7080, 98 | "category_id": 1, 99 | "iscrowd": 0 100 | }, 101 | { 102 | "id": 8585107, 103 | "area": 11363, 104 | "category_id": 1, 105 | "iscrowd": 0 106 | }, 107 | { 108 | "id": 9011200, 109 | "area": 7158, 110 | "category_id": 1, 111 | "iscrowd": 0 112 | }, 113 | { 114 | "id": 12858027, 115 | "area": 6419, 116 | "category_id": 1, 117 | "iscrowd": 0 118 | }, 119 | { 120 | "id": 16053492, 121 | "area": 4350, 122 | "category_id": 1, 123 | "iscrowd": 0 124 | }, 125 | { 126 | "id": 16711680, 127 | "area": 5800, 128 | "category_id": 1, 129 | "iscrowd": 0 130 | } 131 | ] 132 | }, 133 | { 134 | "image_id": 4, 135 | "file_name": "congress.png", 136 | "segments_info": [ 137 | { 138 | "id": 255, 139 | "area": 243, 140 | "category_id": 1, 141 | "iscrowd": 0 142 | }, 143 | { 144 | "id": 65315, 145 | "area": 553, 146 | "category_id": 1, 147 | "iscrowd": 0 148 | }, 149 | { 150 | "id": 65516, 151 | "area": 652, 152 | "category_id": 1, 153 | "iscrowd": 0 154 | }, 155 | { 156 | "id": 9895680, 157 | "area": 82774, 158 | "category_id": 1, 159 | "iscrowd": 1 160 | }, 161 | { 162 | "id": 16711739, 163 | "area": 137, 164 | "category_id": 1, 165 | "iscrowd": 0 166 | }, 167 | { 168 | "id": 16711868, 169 | "area": 179, 170 | "category_id": 1, 171 | "iscrowd": 0 172 | }, 173 | { 174 | "id": 16762624, 175 | "area": 2742, 176 | "category_id": 1, 177 | "iscrowd": 0 178 | } 179 | ] 180 | } 181 | ], 182 | "categories": [ 183 | { 184 | "id": 1, 185 | "name": "person", 186 | "isthing": 1 187 | }, 188 | { 189 | "id": 2, 190 | "name": "umbrella", 191 | "isthing": 1 192 | }, 193 | { 194 | "id": 3, 195 | "name": "tree-merged", 196 | "isthing": 0 197 | }, 198 | { 199 | "id": 4, 200 | "name": "bird", 201 | "isthing": 1 202 | }, 203 | { 204 | "id": 5, 205 | "name": "sky", 206 | "isthing": 0 207 | }, 208 | { 209 | "id": 6, 210 | "name": "cat", 211 | "isthing": 1 212 | } 213 | ] 214 | } 215 | -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_gt/bird.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/coco_gt/bird.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_gt/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/coco_gt/cat.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_gt/congress.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/coco_gt/congress.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_gt/team.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/coco_gt/team.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_pred.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": { 3 | "description": "Test COCO-format dataset", 4 | "url": "https://github.com/tensorflow/models/tree/master/research/deeplab", 5 | "version": "1.0", 6 | "year": 2019 7 | }, 8 | "images": [ 9 | { 10 | "id": 1, 11 | "file_name": "bird.jpg", 12 | "height": 159, 13 | "width": 240, 14 | "flickr_url": "https://www.flickr.com/photos/chivinskia/40619099560" 15 | }, 16 | { 17 | "id": 2, 18 | "file_name": "cat.jpg", 19 | "height": 330, 20 | "width": 317, 21 | "flickr_url": "https://www.flickr.com/photos/magdalena_b/4995858743" 22 | }, 23 | { 24 | "id": 3, 25 | "file_name": "team.jpg", 26 | "height": 231, 27 | "width": 345 28 | }, 29 | { 30 | "id": 4, 31 | "file_name": "congress.jpg", 32 | "height": 267, 33 | "width": 525 34 | } 35 | ], 36 | "annotations": [ 37 | { 38 | "image_id": 1, 39 | "file_name": "bird.png", 40 | "segments_info": [ 41 | { 42 | "id": 55551, 43 | "area": 3039, 44 | "category_id": 4, 45 | "iscrowd": 0 46 | }, 47 | { 48 | "id": 16216831, 49 | "area": 33659, 50 | "category_id": 5, 51 | "iscrowd": 0 52 | }, 53 | { 54 | "id": 16760832, 55 | "area": 1237, 56 | "category_id": 3, 57 | "iscrowd": 0 58 | } 59 | ] 60 | }, 61 | { 62 | "image_id": 2, 63 | "file_name": "cat.png", 64 | "segments_info": [ 65 | { 66 | "id": 36493, 67 | "area": 26910, 68 | "category_id": 6, 69 | "iscrowd": 0 70 | } 71 | ] 72 | }, 73 | { 74 | "image_id": 3, 75 | "file_name": "team.png", 76 | "segments_info": [ 77 | { 78 | "id": 0, 79 | "area": 22164, 80 | "category_id": 1, 81 | "iscrowd": 0 82 | }, 83 | { 84 | "id": 129, 85 | "area": 3418, 86 | "category_id": 1, 87 | "iscrowd": 0 88 | }, 89 | { 90 | "id": 255, 91 | "area": 12827, 92 | "category_id": 1, 93 | "iscrowd": 0 94 | }, 95 | { 96 | "id": 740608, 97 | "area": 8606, 98 | "category_id": 1, 99 | "iscrowd": 0 100 | }, 101 | { 102 | "id": 2555695, 103 | "area": 7636, 104 | "category_id": 1, 105 | "iscrowd": 0 106 | }, 107 | { 108 | "id": 2883541, 109 | "area": 6844, 110 | "category_id": 1, 111 | "iscrowd": 0 112 | }, 113 | { 114 | "id": 14408667, 115 | "area": 4766, 116 | "category_id": 1, 117 | "iscrowd": 0 118 | }, 119 | { 120 | "id": 16711820, 121 | "area": 4767, 122 | "category_id": 1, 123 | "iscrowd": 0 124 | }, 125 | { 126 | "id": 16768768, 127 | "area": 8667, 128 | "category_id": 1, 129 | "iscrowd": 0 130 | } 131 | ] 132 | }, 133 | { 134 | "image_id": 4, 135 | "file_name": "congress.png", 136 | "segments_info": [ 137 | { 138 | "id": 255, 139 | "area": 2599, 140 | "category_id": 1, 141 | "iscrowd": 0 142 | }, 143 | { 144 | "id": 37375, 145 | "area": 386, 146 | "category_id": 1, 147 | "iscrowd": 0 148 | }, 149 | { 150 | "id": 62207, 151 | "area": 384, 152 | "category_id": 1, 153 | "iscrowd": 0 154 | }, 155 | { 156 | "id": 5177088, 157 | "area": 260, 158 | "category_id": 1, 159 | "iscrowd": 0 160 | }, 161 | { 162 | "id": 16711691, 163 | "area": 1011, 164 | "category_id": 1, 165 | "iscrowd": 0 166 | }, 167 | { 168 | "id": 16774912, 169 | "area": 803, 170 | "category_id": 1, 171 | "iscrowd": 0 172 | } 173 | ] 174 | } 175 | ], 176 | "categories": [ 177 | { 178 | "id": 1, 179 | "name": "person", 180 | "isthing": 1 181 | }, 182 | { 183 | "id": 2, 184 | "name": "umbrella", 185 | "isthing": 1 186 | }, 187 | { 188 | "id": 3, 189 | "name": "tree-merged", 190 | "isthing": 0 191 | }, 192 | { 193 | "id": 4, 194 | "name": "bird", 195 | "isthing": 1 196 | }, 197 | { 198 | "id": 5, 199 | "name": "sky", 200 | "isthing": 0 201 | }, 202 | { 203 | "id": 6, 204 | "name": "cat", 205 | "isthing": 1 206 | } 207 | ] 208 | } 209 | -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_pred/bird.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/coco_pred/bird.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_pred/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/coco_pred/cat.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_pred/congress.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/coco_pred/congress.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/coco_pred/team.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/coco_pred/team.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/team_gt_instance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/team_gt_instance.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/team_pred_class.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/team_pred_class.png -------------------------------------------------------------------------------- /research/deeplab/evaluation/testdata/team_pred_instance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/evaluation/testdata/team_pred_instance.png -------------------------------------------------------------------------------- /research/deeplab/g3doc/ade20k.md: -------------------------------------------------------------------------------- 1 | # Running DeepLab on ADE20K Semantic Segmentation Dataset 2 | 3 | This page walks through the steps required to run DeepLab on ADE20K dataset on a 4 | local machine. 5 | 6 | ## Download dataset and convert to TFRecord 7 | 8 | We have prepared the script (under the folder `datasets`) to download and 9 | convert ADE20K semantic segmentation dataset to TFRecord. 10 | 11 | ```bash 12 | # From the tensorflow/models/research/deeplab/datasets directory. 13 | bash download_and_convert_ade20k.sh 14 | ``` 15 | 16 | The converted dataset will be saved at ./deeplab/datasets/ADE20K/tfrecord 17 | 18 | ## Recommended Directory Structure for Training and Evaluation 19 | 20 | ``` 21 | + datasets 22 | - build_data.py 23 | - build_ade20k_data.py 24 | - download_and_convert_ade20k.sh 25 | + ADE20K 26 | + tfrecord 27 | + exp 28 | + train_on_train_set 29 | + train 30 | + eval 31 | + vis 32 | + ADEChallengeData2016 33 | + annotations 34 | + training 35 | + validation 36 | + images 37 | + training 38 | + validation 39 | ``` 40 | 41 | where the folder `train_on_train_set` stores the train/eval/vis events and 42 | results (when training DeepLab on the ADE20K train set). 43 | 44 | ## Running the train/eval/vis jobs 45 | 46 | A local training job using `xception_65` can be run with the following command: 47 | 48 | ```bash 49 | # From tensorflow/models/research/ 50 | python deeplab/train.py \ 51 | --logtostderr \ 52 | --training_number_of_steps=150000 \ 53 | --train_split="train" \ 54 | --model_variant="xception_65" \ 55 | --atrous_rates=6 \ 56 | --atrous_rates=12 \ 57 | --atrous_rates=18 \ 58 | --output_stride=16 \ 59 | --decoder_output_stride=4 \ 60 | --train_crop_size="513,513" \ 61 | --train_batch_size=4 \ 62 | --min_resize_value=513 \ 63 | --max_resize_value=513 \ 64 | --resize_factor=16 \ 65 | --dataset="ade20k" \ 66 | --tf_initial_checkpoint=${PATH_TO_INITIAL_CHECKPOINT} \ 67 | --train_logdir=${PATH_TO_TRAIN_DIR}\ 68 | --dataset_dir=${PATH_TO_DATASET} 69 | ``` 70 | 71 | where ${PATH\_TO\_INITIAL\_CHECKPOINT} is the path to the initial checkpoint. 72 | ${PATH\_TO\_TRAIN\_DIR} is the directory in which training checkpoints and 73 | events will be written to (it is recommended to set it to the 74 | `train_on_train_set/train` above), and ${PATH\_TO\_DATASET} is the directory in 75 | which the ADE20K dataset resides (the `tfrecord` above) 76 | 77 | **Note that for train.py:** 78 | 79 | 1. In order to fine tune the BN layers, one needs to use large batch size (> 80 | 12), and set fine_tune_batch_norm = True. Here, we simply use small batch 81 | size during training for the purpose of demonstration. If the users have 82 | limited GPU memory at hand, please fine-tune from our provided checkpoints 83 | whose batch norm parameters have been trained, and use smaller learning rate 84 | with fine_tune_batch_norm = False. 85 | 86 | 2. User should fine tune the `min_resize_value` and `max_resize_value` to get 87 | better result. Note that `resize_factor` has to be equal to `output_stride`. 88 | 89 | 3. The users should change atrous_rates from [6, 12, 18] to [12, 24, 36] if 90 | setting output_stride=8. 91 | 92 | 4. The users could skip the flag, `decoder_output_stride`, if you do not want 93 | to use the decoder structure. 94 | 95 | ## Running Tensorboard 96 | 97 | Progress for training and evaluation jobs can be inspected using Tensorboard. If 98 | using the recommended directory structure, Tensorboard can be run using the 99 | following command: 100 | 101 | ```bash 102 | tensorboard --logdir=${PATH_TO_LOG_DIRECTORY} 103 | ``` 104 | 105 | where `${PATH_TO_LOG_DIRECTORY}` points to the directory that contains the train 106 | directorie (e.g., the folder `train_on_train_set` in the above example). Please 107 | note it may take Tensorboard a couple minutes to populate with data. 108 | -------------------------------------------------------------------------------- /research/deeplab/g3doc/export_model.md: -------------------------------------------------------------------------------- 1 | # Export trained deeplab model to frozen inference graph 2 | 3 | After model training finishes, you could export it to a frozen TensorFlow 4 | inference graph proto. Your trained model checkpoint usually includes the 5 | following files: 6 | 7 | * model.ckpt-${CHECKPOINT_NUMBER}.data-00000-of-00001, 8 | * model.ckpt-${CHECKPOINT_NUMBER}.index 9 | * model.ckpt-${CHECKPOINT_NUMBER}.meta 10 | 11 | After you have identified a candidate checkpoint to export, you can run the 12 | following commandline to export to a frozen graph: 13 | 14 | ```bash 15 | # From tensorflow/models/research/ 16 | # Assume all checkpoint files share the same path prefix `${CHECKPOINT_PATH}`. 17 | python deeplab/export_model.py \ 18 | --checkpoint_path=${CHECKPOINT_PATH} \ 19 | --export_path=${OUTPUT_DIR}/frozen_inference_graph.pb 20 | ``` 21 | 22 | Please also add other model specific flags as you use for training, such as 23 | `model_variant`, `add_image_level_feature`, etc. 24 | -------------------------------------------------------------------------------- /research/deeplab/g3doc/faq.md: -------------------------------------------------------------------------------- 1 | # FAQ 2 | ___ 3 | Q1: What if I want to use other network backbones, such as ResNet [1], instead of only those provided ones (e.g., Xception)? 4 | 5 | A: The users could modify the provided core/feature_extractor.py to support more network backbones. 6 | ___ 7 | Q2: What if I want to train the model on other datasets? 8 | 9 | A: The users could modify the provided dataset/build_{cityscapes,voc2012}_data.py and dataset/segmentation_dataset.py to build their own dataset. 10 | ___ 11 | Q3: Where can I download the PASCAL VOC augmented training set? 12 | 13 | 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. 14 | ___ 15 | Q4: Why the implementation does not include DenseCRF [3]? 16 | 17 | 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. 18 | ___ 19 | Q5: What if I want to train the model and fine-tune the batch normalization parameters? 20 | 21 | A: If given the limited resource at hand, we would suggest you simply fine-tune 22 | from our provided checkpoint whose batch-norm parameters have been trained (i.e., 23 | train with a smaller learning rate, set `fine_tune_batch_norm = false`, and 24 | employ longer training iterations since the learning rate is small). If 25 | you really would like to train by yourself, we would suggest 26 | 27 | 1. Set `output_stride = 16` or maybe even `32` (remember to change the flag 28 | `atrous_rates` accordingly, e.g., `atrous_rates = [3, 6, 9]` for 29 | `output_stride = 32`). 30 | 31 | 2. Use as many GPUs as possible (change the flag `num_clones` in train.py) and 32 | set `train_batch_size` as large as possible. 33 | 34 | 3. Adjust the `train_crop_size` in train.py. Maybe set it to be smaller, e.g., 35 | 513x513 (or even 321x321), so that you could use a larger batch size. 36 | 37 | 4. Use a smaller network backbone, such as MobileNet-v2. 38 | 39 | ___ 40 | Q6: How can I train the model asynchronously? 41 | 42 | 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. 43 | ___ 44 | Q7: I could not reproduce the performance even with the provided checkpoints. 45 | 46 | A: Please try running 47 | 48 | ```bash 49 | # Run the simple test with Xception_65 as network backbone. 50 | sh local_test.sh 51 | ``` 52 | 53 | or 54 | 55 | ```bash 56 | # Run the simple test with MobileNet-v2 as network backbone. 57 | sh local_test_mobilenetv2.sh 58 | ``` 59 | 60 | First, make sure you could reproduce the results with our provided setting. 61 | After that, you could start to make a new change one at a time to help debug. 62 | ___ 63 | Q8: What value of `eval_crop_size` should I use? 64 | 65 | 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 66 | 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 67 | image dimension is all equal to 1024x2048. 68 | ___ 69 | Q9: Why multi-gpu training is slow? 70 | 71 | 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/utils/input_generator.py#L71) and [num_threads = 4](https://github.com/tensorflow/models/blob/master/research/deeplab/utils/input_generator.py#L72). 72 | ___ 73 | 74 | 75 | ## References 76 | 77 | 1. **Deep Residual Learning for Image Recognition**
78 | Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
79 | [[link]](https://arxiv.org/abs/1512.03385), In CVPR, 2016. 80 | 81 | 2. **Semantic Contours from Inverse Detectors**
82 | Bharath Hariharan, Pablo Arbelaez, Lubomir Bourdev, Subhransu Maji, Jitendra Malik
83 | [[link]](http://home.bharathh.info/pubs/codes/SBD/download.html), In ICCV, 2011. 84 | 85 | 3. **Efficient Inference in Fully Connected CRFs with Gaussian Edge Potentials**
86 | Philipp Krähenbühl, Vladlen Koltun
87 | [[link]](http://graphics.stanford.edu/projects/densecrf/), In NIPS, 2011. 88 | -------------------------------------------------------------------------------- /research/deeplab/g3doc/img/image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/g3doc/img/image1.jpg -------------------------------------------------------------------------------- /research/deeplab/g3doc/img/image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/g3doc/img/image2.jpg -------------------------------------------------------------------------------- /research/deeplab/g3doc/img/image3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/g3doc/img/image3.jpg -------------------------------------------------------------------------------- /research/deeplab/g3doc/img/image_info.txt: -------------------------------------------------------------------------------- 1 | Image provenance: 2 | 3 | image1.jpg: Philippe Put, 4 | https://www.flickr.com/photos/34547181@N00/14499172124 5 | 6 | image2.jpg: Peretz Partensky 7 | https://www.flickr.com/photos/ifl/3926001309 8 | 9 | image3.jpg: Peter Harrison 10 | https://www.flickr.com/photos/devcentre/392585679 11 | 12 | 13 | vis[1-3].png: Showing original image together with DeepLab segmentation map. 14 | -------------------------------------------------------------------------------- /research/deeplab/g3doc/img/vis1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/g3doc/img/vis1.png -------------------------------------------------------------------------------- /research/deeplab/g3doc/img/vis2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/g3doc/img/vis2.png -------------------------------------------------------------------------------- /research/deeplab/g3doc/img/vis3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/g3doc/img/vis3.png -------------------------------------------------------------------------------- /research/deeplab/g3doc/installation.md: -------------------------------------------------------------------------------- 1 | # Installation 2 | 3 | ## Dependencies 4 | 5 | DeepLab depends on the following libraries: 6 | 7 | * Numpy 8 | * Pillow 1.0 9 | * tf Slim (which is included in the "tensorflow/models/research/" checkout) 10 | * Jupyter notebook 11 | * Matplotlib 12 | * Tensorflow 13 | 14 | For detailed steps to install Tensorflow, follow the [Tensorflow installation 15 | instructions](https://www.tensorflow.org/install/). A typical user can install 16 | Tensorflow using one of the following commands: 17 | 18 | ```bash 19 | # For CPU 20 | pip install tensorflow 21 | # For GPU 22 | pip install tensorflow-gpu 23 | ``` 24 | 25 | The remaining libraries can be installed on Ubuntu 14.04 using via apt-get: 26 | 27 | ```bash 28 | sudo apt-get install python-pil python-numpy 29 | pip install --user jupyter 30 | pip install --user matplotlib 31 | pip install --user PrettyTable 32 | ``` 33 | 34 | ## Add Libraries to PYTHONPATH 35 | 36 | When running locally, the tensorflow/models/research/ directory should be 37 | appended to PYTHONPATH. This can be done by running the following from 38 | tensorflow/models/research/: 39 | 40 | ```bash 41 | # From tensorflow/models/research/ 42 | export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim 43 | 44 | # [Optional] for panoptic evaluation, you might need panopticapi: 45 | # https://github.com/cocodataset/panopticapi 46 | # Please clone it to a local directory ${PANOPTICAPI_DIR} 47 | touch ${PANOPTICAPI_DIR}/panopticapi/__init__.py 48 | export PYTHONPATH=$PYTHONPATH:${PANOPTICAPI_DIR}/panopticapi 49 | ``` 50 | 51 | Note: This command needs to run from every new terminal you start. If you wish 52 | to avoid running this manually, you can add it as a new line to the end of your 53 | ~/.bashrc file. 54 | 55 | # Testing the Installation 56 | 57 | You can test if you have successfully installed the Tensorflow DeepLab by 58 | running the following commands: 59 | 60 | Quick test by running model_test.py: 61 | 62 | ```bash 63 | # From tensorflow/models/research/ 64 | python deeplab/model_test.py 65 | ``` 66 | 67 | Quick running the whole code on the PASCAL VOC 2012 dataset: 68 | 69 | ```bash 70 | # From tensorflow/models/research/deeplab 71 | sh local_test.sh 72 | ``` 73 | 74 | -------------------------------------------------------------------------------- /research/deeplab/local_test_mobilenetv2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Modified by Dmitrii Marin, https://github.com/dmitrii-marin 3 | # 4 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ============================================================================== 18 | # 19 | # This script is used to run local test on PASCAL VOC 2012 using MobileNet-v2. 20 | # Users could also modify from this script for their use case. 21 | # 22 | # Usage: 23 | # # From the tensorflow/models/research/deeplab directory. 24 | # sh ./local_test_mobilenetv2.sh 25 | # 26 | # 27 | 28 | # Exit immediately if a command exits with a non-zero status. 29 | set -e 30 | 31 | # Move one-level up to tensorflow/models/research directory. 32 | cd .. 33 | 34 | # Update PYTHONPATH. 35 | export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim 36 | 37 | # Set up the working environment. 38 | CURRENT_DIR=$(pwd) 39 | WORK_DIR="${CURRENT_DIR}/deeplab" 40 | 41 | # Run model_test first to make sure the PYTHONPATH is correctly set. 42 | # python "${WORK_DIR}"/model_test.py -v 43 | 44 | # Go to datasets folder and download PASCAL VOC 2012 segmentation dataset. 45 | DATASET_DIR="datasets" 46 | cd "${WORK_DIR}/${DATASET_DIR}" 47 | # sh download_and_convert_voc2012.sh 48 | 49 | # Go back to original directory. 50 | cd "${CURRENT_DIR}" 51 | 52 | # Set up the working directories. 53 | PASCAL_FOLDER="cityscapes" 54 | EXP_FOLDER="exp/train_on_train_set_mobilenetv2" 55 | INIT_FOLDER="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/init_models" 56 | TRAIN_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/train" 57 | EVAL_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/eval" 58 | VIS_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/vis" 59 | EXPORT_DIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/export" 60 | mkdir -p "${INIT_FOLDER}" 61 | mkdir -p "${TRAIN_LOGDIR}" 62 | mkdir -p "${EVAL_LOGDIR}" 63 | mkdir -p "${VIS_LOGDIR}" 64 | mkdir -p "${EXPORT_DIR}" 65 | 66 | # Copy locally the trained checkpoint as the initial checkpoint. 67 | TF_INIT_ROOT="http://download.tensorflow.org/models" 68 | #CKPT_NAME="deeplabv3_mnv2i_pascal_train_aug" 69 | CKPT_NAME="deeplabv3_mnv2_cityscapes_train" 70 | TF_INIT_CKPT="${CKPT_NAME}_2018_02_05.tar.gz" 71 | #TF_INIT_CKPT="${CKPT_NAME}_2018_01_29.tar.gz" 72 | cd "${INIT_FOLDER}" 73 | wget -nd -c "${TF_INIT_ROOT}/${TF_INIT_CKPT}" 74 | tar -xf "${TF_INIT_CKPT}" 75 | cd "${CURRENT_DIR}" 76 | 77 | PASCAL_DATASET="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/tfrecord" 78 | 79 | # Train 10 iterations. 80 | NUM_ITERATIONS=10 81 | python "${WORK_DIR}"/train.py \ 82 | --logtostderr \ 83 | --dataset="cityscapes" \ 84 | --train_split="train" \ 85 | --model_variant="mobilenet_v2" \ 86 | --output_stride=16 \ 87 | --train_crop_size="513,513" \ 88 | --train_batch_size=4 \ 89 | --training_number_of_steps="${NUM_ITERATIONS}" \ 90 | --fine_tune_batch_norm=true \ 91 | --tf_initial_checkpoint="${INIT_FOLDER}/${CKPT_NAME}/model.ckpt" \ 92 | --train_logdir="${TRAIN_LOGDIR}" \ 93 | --dataset_dir="${PASCAL_DATASET}" 94 | 95 | # Run evaluation. This performs eval over the full val split (1449 images) and 96 | # will take a while. 97 | # Using the provided checkpoint, one should expect mIOU=75.34%. 98 | python "${WORK_DIR}"/eval.py \ 99 | --logtostderr \ 100 | --dataset="cityscapes" \ 101 | --eval_split="val" \ 102 | --model_variant="mobilenet_v2" \ 103 | --eval_crop_size="1025,2049" \ 104 | --checkpoint_dir="${TRAIN_LOGDIR}" \ 105 | --eval_logdir="${EVAL_LOGDIR}" \ 106 | --dataset_dir="${PASCAL_DATASET}" \ 107 | --max_number_of_evaluations=1 108 | 109 | # Visualize the results. 110 | python "${WORK_DIR}"/vis.py \ 111 | --logtostderr \ 112 | --dataset="cityscapes" \ 113 | --vis_split="val" \ 114 | --model_variant="mobilenet_v2" \ 115 | --vis_crop_size="1025,2049" \ 116 | --checkpoint_dir="${TRAIN_LOGDIR}" \ 117 | --vis_logdir="${VIS_LOGDIR}" \ 118 | --dataset_dir="${PASCAL_DATASET}" \ 119 | --max_number_of_iterations=1 120 | 121 | # Export the trained checkpoint. 122 | CKPT_PATH="${TRAIN_LOGDIR}/model.ckpt-${NUM_ITERATIONS}" 123 | EXPORT_PATH="${EXPORT_DIR}/frozen_inference_graph.pb" 124 | 125 | python "${WORK_DIR}"/export_model.py \ 126 | --logtostderr \ 127 | --dataset="cityscapes" \ 128 | --checkpoint_path="${CKPT_PATH}" \ 129 | --export_path="${EXPORT_PATH}" \ 130 | --model_variant="mobilenet_v2" \ 131 | --num_classes=21 \ 132 | --crop_size=513 \ 133 | --crop_size=513 \ 134 | --inference_scales=1.0 135 | 136 | # Run inference with the exported checkpoint. 137 | # Please refer to the provided deeplab_demo.ipynb for an example. 138 | -------------------------------------------------------------------------------- /research/deeplab/nus_common.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Modified by Dmitrii Marin, https://github.com/dmitrii-marin 3 | # 4 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ============================================================================== 18 | # 19 | # This script is used to run local test on PASCAL VOC 2012 using MobileNet-v2. 20 | # Users could also modify from this script for their use case. 21 | # 22 | # Usage: 23 | # # From the tensorflow/models/research/deeplab directory. 24 | # sh ./local_test_mobilenetv2.sh 25 | # 26 | # 27 | 28 | # Exit immediately if a command exits with a non-zero status. 29 | set -e 30 | 31 | # Move one-level up to tensorflow/models/research directory. 32 | cd .. 33 | 34 | # Update PYTHONPATH. 35 | export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim 36 | 37 | # Set up the working environment. 38 | CURRENT_DIR=$(pwd) 39 | WORK_DIR="${CURRENT_DIR}/deeplab" 40 | 41 | # Run model_test first to make sure the PYTHONPATH is correctly set. 42 | # python "${WORK_DIR}"/model_test.py -v 43 | 44 | # Go to datasets folder and download PASCAL VOC 2012 segmentation dataset. 45 | DATASET_DIR="datasets" 46 | cd "${WORK_DIR}/${DATASET_DIR}" 47 | # sh download_and_convert_voc2012.sh 48 | 49 | # Go back to original directory. 50 | cd "${CURRENT_DIR}" 51 | 52 | LEARNING_RATE=0.007 53 | DEPTH_MULTIPLIER=0.25 54 | AUX_INPUT_SIZE=64 55 | [ -n "${SAMPLING_SIZE}" ] || SAMPLING_SIZE=128 56 | STRIDE=$((AUX_INPUT_SIZE/8)) 57 | -------------------------------------------------------------------------------- /research/deeplab/test_mobilenetv2_with_nus.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Modified by Dmitrii Marin, https://github.com/dmitrii-marin 3 | # 4 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ============================================================================== 18 | # 19 | # This script is used to run local test on PASCAL VOC 2012 using MobileNet-v2. 20 | # Users could also modify from this script for their use case. 21 | # 22 | # Usage: 23 | # # From the tensorflow/models/research/deeplab directory. 24 | # sh ./local_test_mobilenetv2.sh 25 | # 26 | # 27 | 28 | source "nus_common.sh" 29 | 30 | # Set up the working directories. 31 | PASCAL_FOLDER="cityscapes" 32 | EXP_FOLDER="exp/train_on_train_set_mobilenetv2_w_nus_at_${SAMPLING_SIZE}" 33 | INIT_FOLDER="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/init_models" 34 | TRAIN_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/train" 35 | EVAL_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/eval" 36 | VIS_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/vis" 37 | EXPORT_DIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/export" 38 | 39 | if [ -z ${UNIFORM} ]; then 40 | NUS_PREPROCES_TYPE="net" 41 | else 42 | NUS_PREPROCES_TYPE="uniform" 43 | TRAIN_LOGDIR="${TRAIN_LOGDIR}_${NUS_PREPROCES_TYPE}" 44 | EVAL_LOGDIR="${EVAL_LOGDIR}_${NUS_PREPROCES_TYPE}" 45 | VIS_LOGDIR="${VIS_LOGDIR}_${NUS_PREPROCES_TYPE}" 46 | EXPORT_DIR="${EXPORT_DIR}_${NUS_PREPROCES_TYPE}" 47 | fi 48 | 49 | mkdir -p "${INIT_FOLDER}" 50 | mkdir -p "${TRAIN_LOGDIR}" 51 | mkdir -p "${EVAL_LOGDIR}" 52 | mkdir -p "${VIS_LOGDIR}" 53 | mkdir -p "${EXPORT_DIR}" 54 | 55 | if [ -z ${MODE} ]; then MODE=TRAIN; fi 56 | 57 | if [ ${MODE} == TRAIN ]; then 58 | # Copy locally the trained checkpoint as the initial checkpoint. 59 | # https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_128.tgz 60 | TF_INIT_ROOT="https://storage.googleapis.com/mobilenet_v2/checkpoints" 61 | #CKPT_NAME="deeplabv3_mnv2i_pascal_train_aug" 62 | CKPT_NAME="mobilenet_v2_1.0_${SAMPLING_SIZE}" 63 | TF_INIT_CKPT="${CKPT_NAME}.tgz" 64 | #TF_INIT_CKPT="${CKPT_NAME}_2018_01_29.tar.gz" 65 | cd "${INIT_FOLDER}" 66 | wget -nd -c "${TF_INIT_ROOT}/${TF_INIT_CKPT}" 67 | tar -xf "${TF_INIT_CKPT}" 68 | cd "${CURRENT_DIR}" 69 | fi 70 | 71 | PASCAL_DATASET="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/tfrecord" 72 | 73 | NUS_CHECKPOINT="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/exp/nus64_128_mobilenetv2_lr0.007_mt0.25/train/model.ckpt-30000" 74 | 75 | 76 | 77 | if [ ${MODE} == "TRAIN" ]; then 78 | # Train 10 iterations. 79 | NUM_ITERATIONS=90000 80 | python "${WORK_DIR}"/train.py \ 81 | --logtostderr \ 82 | --dataset="cityscapes" \ 83 | --train_split="train" \ 84 | --model_variant="mobilenet_v2" \ 85 | --base_learning_rate=0.007 \ 86 | --nus_preprocess="${NUS_PREPROCES_TYPE}" \ 87 | --nus_net_input_size=${AUX_INPUT_SIZE} \ 88 | --nus_net_stride=${STRIDE} \ 89 | --nus_sampling_size=${SAMPLING_SIZE} \ 90 | --nus_depth_multiplier=${DEPTH_MULTIPLIER} \ 91 | --nus_checkpoint="${NUS_CHECKPOINT}" \ 92 | --min_scale_factor=1 \ 93 | --max_scale_factor=1 \ 94 | --train_batch_size=16 \ 95 | --train_crop_size="1024,1024" \ 96 | --training_number_of_steps="${NUM_ITERATIONS}" \ 97 | --fine_tune_batch_norm=true \ 98 | --train_logdir="${TRAIN_LOGDIR}" \ 99 | --dataset_dir="${PASCAL_DATASET}" \ 100 | --tf_initial_checkpoint="${INIT_FOLDER}/${CKPT_NAME}.ckpt" \ 101 | --save_summaries_images=True \ 102 | 103 | elif [ ${MODE} == "EVAL" ]; then 104 | 105 | python "${WORK_DIR}"/eval.py \ 106 | --logtostderr \ 107 | --dataset="cityscapes" \ 108 | --eval_split="val" \ 109 | --model_variant="mobilenet_v2" \ 110 | --eval_crop_size="1024,1024" \ 111 | --output_stride=8 \ 112 | --checkpoint_dir="${TRAIN_LOGDIR}" \ 113 | --eval_logdir="${EVAL_LOGDIR}" \ 114 | --dataset_dir="${PASCAL_DATASET}" \ 115 | --max_number_of_evaluations=-1 \ 116 | --nus_preprocess="${NUS_PREPROCES_TYPE}" \ 117 | --nus_net_input_size=${AUX_INPUT_SIZE} \ 118 | --nus_net_stride=${STRIDE} \ 119 | --nus_sampling_size=${SAMPLING_SIZE} \ 120 | --nus_depth_multiplier=${DEPTH_MULTIPLIER} \ 121 | --nus_checkpoint="${NUS_CHECKPOINT}" \ 122 | 123 | fi 124 | -------------------------------------------------------------------------------- /research/deeplab/test_nus_mobilenetv2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Modified by Dmitrii Marin, https://github.com/dmitrii-marin 3 | # 4 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ============================================================================== 18 | # 19 | # This script is used to run local test on PASCAL VOC 2012 using MobileNet-v2. 20 | # Users could also modify from this script for their use case. 21 | # 22 | # Usage: 23 | # # From the tensorflow/models/research/deeplab directory. 24 | # sh ./local_test_mobilenetv2.sh 25 | # 26 | # 27 | 28 | source "nus_common.sh" 29 | 30 | PASCAL_FOLDER="cityscapes" 31 | EXP_FOLDER="exp/nus${AUX_INPUT_SIZE}_128_mobilenetv2_lr${LEARNING_RATE}_mt${DEPTH_MULTIPLIER}" 32 | INIT_FOLDER="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/init_models" 33 | TRAIN_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/train" 34 | EVAL_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/eval" 35 | VIS_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/vis" 36 | EXPORT_DIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/export" 37 | mkdir -p "${INIT_FOLDER}" 38 | mkdir -p "${TRAIN_LOGDIR}" 39 | mkdir -p "${EVAL_LOGDIR}" 40 | mkdir -p "${VIS_LOGDIR}" 41 | mkdir -p "${EXPORT_DIR}" 42 | 43 | # Copy locally the trained checkpoint as the initial checkpoint. 44 | TF_INIT_ROOT="http://download.tensorflow.org/models" 45 | #CKPT_NAME="deeplabv3_mnv2i_pascal_train_aug" 46 | CKPT_NAME="deeplabv3_mnv2_cityscapes_train" 47 | TF_INIT_CKPT="${CKPT_NAME}_2018_02_05.tar.gz" 48 | #TF_INIT_CKPT="${CKPT_NAME}_2018_01_29.tar.gz" 49 | cd "${INIT_FOLDER}" 50 | wget -nd -c "${TF_INIT_ROOT}/${TF_INIT_CKPT}" 51 | tar -xf "${TF_INIT_CKPT}" 52 | cd "${CURRENT_DIR}" 53 | 54 | PASCAL_DATASET="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/tfrecord" 55 | 56 | 57 | # Train 10 iterations. 58 | NUM_ITERATIONS=30000 59 | python "${WORK_DIR}"/train.py \ 60 | --logtostderr \ 61 | --dataset="cityscapes" \ 62 | --train_split="train" \ 63 | --base_learning_rate=${LEARNING_RATE} \ 64 | --model_variant="mobilenet_v2" \ 65 | --nus_type="net" \ 66 | --nus_net_input_size=${AUX_INPUT_SIZE} \ 67 | --nus_net_stride=${STRIDE} \ 68 | --nus_sampling_size=${SAMPLING_SIZE} \ 69 | --nus_depth_multiplier=${DEPTH_MULTIPLIER} \ 70 | --nus_train=True \ 71 | --max_resize_value=${AUX_INPUT_SIZE} \ 72 | --min_resize_value=${AUX_INPUT_SIZE} \ 73 | --nus_target_classes="11,12,13,14,15,16,17,18" \ 74 | --min_scale_factor=1 \ 75 | --max_scale_factor=1 \ 76 | --train_batch_size=32 \ 77 | --train_crop_size="1024,1024" \ 78 | --training_number_of_steps="${NUM_ITERATIONS}" \ 79 | --fine_tune_batch_norm=true \ 80 | --train_logdir="${TRAIN_LOGDIR}" \ 81 | --dataset_dir="${PASCAL_DATASET}" \ 82 | --save_summaries_images=True 83 | 84 | -------------------------------------------------------------------------------- /research/deeplab/testing/info.md: -------------------------------------------------------------------------------- 1 | This directory contains testing data. 2 | 3 | # pascal_voc_seg 4 | This folder contains data specific to pascal_voc_seg dataset. val-00000-of-00001.tfrecord contains 5 | three randomly generated images with format defined in 6 | tensorflow/models/research/deeplab/datasets/build_voc2012_data.py. 7 | -------------------------------------------------------------------------------- /research/deeplab/testing/pascal_voc_seg/val-00000-of-00001.tfrecord: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/testing/pascal_voc_seg/val-00000-of-00001.tfrecord -------------------------------------------------------------------------------- /research/deeplab/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/deeplab/utils/__init__.py -------------------------------------------------------------------------------- /research/deeplab/utils/get_dataset_colormap_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for get_dataset_colormap.py.""" 17 | 18 | import numpy as np 19 | import tensorflow as tf 20 | 21 | from deeplab.utils import get_dataset_colormap 22 | 23 | 24 | class VisualizationUtilTest(tf.test.TestCase): 25 | 26 | def testBitGet(self): 27 | """Test that if the returned bit value is correct.""" 28 | self.assertEqual(1, get_dataset_colormap.bit_get(9, 0)) 29 | self.assertEqual(0, get_dataset_colormap.bit_get(9, 1)) 30 | self.assertEqual(0, get_dataset_colormap.bit_get(9, 2)) 31 | self.assertEqual(1, get_dataset_colormap.bit_get(9, 3)) 32 | 33 | def testPASCALLabelColorMapValue(self): 34 | """Test the getd color map value.""" 35 | colormap = get_dataset_colormap.create_pascal_label_colormap() 36 | 37 | # Only test a few sampled entries in the color map. 38 | self.assertTrue(np.array_equal([128., 0., 128.], colormap[5, :])) 39 | self.assertTrue(np.array_equal([128., 192., 128.], colormap[23, :])) 40 | self.assertTrue(np.array_equal([128., 0., 192.], colormap[37, :])) 41 | self.assertTrue(np.array_equal([224., 192., 192.], colormap[127, :])) 42 | self.assertTrue(np.array_equal([192., 160., 192.], colormap[175, :])) 43 | 44 | def testLabelToPASCALColorImage(self): 45 | """Test the value of the converted label value.""" 46 | label = np.array([[0, 16, 16], [52, 7, 52]]) 47 | expected_result = np.array([ 48 | [[0, 0, 0], [0, 64, 0], [0, 64, 0]], 49 | [[0, 64, 192], [128, 128, 128], [0, 64, 192]] 50 | ]) 51 | colored_label = get_dataset_colormap.label_to_color_image( 52 | label, get_dataset_colormap.get_pascal_name()) 53 | self.assertTrue(np.array_equal(expected_result, colored_label)) 54 | 55 | def testUnExpectedLabelValueForLabelToPASCALColorImage(self): 56 | """Raise ValueError when input value exceeds range.""" 57 | label = np.array([[120], [300]]) 58 | with self.assertRaises(ValueError): 59 | get_dataset_colormap.label_to_color_image( 60 | label, get_dataset_colormap.get_pascal_name()) 61 | 62 | def testUnExpectedLabelDimensionForLabelToPASCALColorImage(self): 63 | """Raise ValueError if input dimension is not correct.""" 64 | label = np.array([120]) 65 | with self.assertRaises(ValueError): 66 | get_dataset_colormap.label_to_color_image( 67 | label, get_dataset_colormap.get_pascal_name()) 68 | 69 | def testGetColormapForUnsupportedDataset(self): 70 | with self.assertRaises(ValueError): 71 | get_dataset_colormap.create_label_colormap('unsupported_dataset') 72 | 73 | def testUnExpectedLabelDimensionForLabelToADE20KColorImage(self): 74 | label = np.array([250]) 75 | with self.assertRaises(ValueError): 76 | get_dataset_colormap.label_to_color_image( 77 | label, get_dataset_colormap.get_ade20k_name()) 78 | 79 | def testFirstColorInADE20KColorMap(self): 80 | label = np.array([[1, 3], [10, 20]]) 81 | expected_result = np.array([ 82 | [[120, 120, 120], [6, 230, 230]], 83 | [[4, 250, 7], [204, 70, 3]] 84 | ]) 85 | colored_label = get_dataset_colormap.label_to_color_image( 86 | label, get_dataset_colormap.get_ade20k_name()) 87 | self.assertTrue(np.array_equal(colored_label, expected_result)) 88 | 89 | def testMapillaryVistasColorMapValue(self): 90 | colormap = get_dataset_colormap.create_mapillary_vistas_label_colormap() 91 | self.assertTrue(np.array_equal([190, 153, 153], colormap[3, :])) 92 | self.assertTrue(np.array_equal([102, 102, 156], colormap[6, :])) 93 | 94 | 95 | if __name__ == '__main__': 96 | tf.test.main() 97 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | cmake_minimum_required(VERSION 3.0) 17 | 18 | set(CMAKE_INCLUDE_CURRENT_DIR on) 19 | set(CMAKE_CXX_STANDARD 11) 20 | 21 | include(UseCython.cmake) 22 | find_package( Ceres REQUIRED ) 23 | 24 | set( PYTHON_INCLUDE_DIR ~/anaconda3/include/python3.6m) 25 | find_package( PythonLibs 3 REQUIRED ) 26 | 27 | include_directories(${PYTHON_INCLUDE_DIRS}) 28 | include_directories(${CERES_INCLUDE_DIRS}) 29 | 30 | CYTHON_ADD_MODULE(closest_point_cython closest_point.cpp ) 31 | target_link_libraries(closest_point_cython ceres) 32 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/UseCython.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | # This allows to link Cython files 17 | # Examples: 18 | # 1) to compile assembly.pyx to assembly.so: 19 | # CYTHON_ADD_MODULE(assembly) 20 | # 2) to compile assembly.pyx and something.cpp to assembly.so: 21 | # CYTHON_ADD_MODULE(assembly something.cpp) 22 | 23 | find_program(CYTHON NAMES cython cython.py) 24 | 25 | if(NOT CYTHON_INCLUDE_DIRECTORIES) 26 | set(CYTHON_INCLUDE_DIRECTORIES .) 27 | endif(NOT CYTHON_INCLUDE_DIRECTORIES) 28 | 29 | macro(CYTHON_ADD_MODULE name) 30 | 31 | add_custom_command( 32 | OUTPUT ${name}.cpp 33 | COMMAND ${CYTHON} 34 | ARGS -3 -I ${CYTHON_INCLUDE_DIRECTORIES} -o ${name}.cpp ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pyx 35 | DEPENDS ${name}.pyx 36 | COMMENT "Cython source") 37 | 38 | add_library(${name} MODULE ${name}.cpp ${ARGN}) 39 | 40 | set_target_properties(${name} PROPERTIES PREFIX "") 41 | if (CMAKE_HOST_WIN32) 42 | set_target_properties(${name} PROPERTIES SUFFIX ".pyd") 43 | endif(CMAKE_HOST_WIN32) 44 | 45 | endmacro(CYTHON_ADD_MODULE) 46 | 47 | macro(CYTHON_COPY name) 48 | 49 | get_target_property(FILEPATH ${name} LOCATION) 50 | 51 | add_custom_command(TARGET ${name} POST_BUILD 52 | COMMAND ${CMAKE_COMMAND} 53 | ARGS -E copy ${FILEPATH} ${CMAKE_CURRENT_SOURCE_DIR} 54 | COMMENT "Copy python module to source dir" 55 | ) 56 | 57 | endmacro(CYTHON_COPY) 58 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | from .sample import * 17 | from .utils import * 18 | from .interpolation import * 19 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | 17 | 18 | if ! [ -e build ]; then 19 | mkdir build 20 | fi 21 | 22 | cd build 23 | cmake .. 24 | make 25 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/closest_point.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | */ 17 | 18 | #include "closest_point.h" 19 | 20 | #include "ceres/ceres.h" 21 | #include "glog/logging.h" 22 | 23 | using ceres::AutoDiffCostFunction; 24 | using ceres::CostFunction; 25 | using ceres::Problem; 26 | using ceres::Solver; 27 | using ceres::Solve; 28 | 29 | template 30 | struct D { 31 | template 32 | static void diff(const T* x, const R* y, T* z, double w) { 33 | static_assert(Dim>0, "Dim>0!"); 34 | z[0] = w * (x[0] - y[0]); 35 | D::diff(x + 1, y + 1, z + 1, w); 36 | } 37 | }; 38 | 39 | 40 | template<> 41 | struct D<0> { 42 | template 43 | static void diff(const T* x, const R* y, T* z, double w) { } 44 | }; 45 | 46 | 47 | template 48 | struct UnaryDiff { 49 | template 50 | bool operator()(const T* const x, T* residual) const { 51 | D::diff(x, ptr, residual, weight); 52 | return true; 53 | } 54 | 55 | const double* ptr; 56 | double weight; 57 | }; 58 | 59 | 60 | template 61 | struct PairwiseDiff { 62 | template 63 | bool operator()(const T* const x, const T* const y, T* residual) const { 64 | D::diff(x, y, residual, weight); 65 | return true; 66 | } 67 | double weight; 68 | }; 69 | 70 | 71 | void closest_point(const double* target, int h, int w, double weight, double* out) { 72 | double w_u = sqrt(weight); 73 | double w_p = sqrt(1 - w_u); 74 | Problem problem; 75 | 76 | // energy terms 77 | for (int i = 0; i < h; ++i) 78 | for (int j = 0; j < w; ++j) { 79 | // distance to boundary 80 | CostFunction* cost_function = 81 | new AutoDiffCostFunction, 2, 2>( 82 | new UnaryDiff<2> {target + (i * w + j) * 2, w_u}); 83 | problem.AddResidualBlock(cost_function, NULL, out + (i * w + j) * 2); 84 | 85 | // distance to neighbours 86 | if (i + 1 < h) { 87 | cost_function = 88 | new AutoDiffCostFunction, 2, 2, 2>( 89 | new PairwiseDiff<2>{w_p}); 90 | problem.AddResidualBlock(cost_function, NULL, 91 | out + (i * w + j) * 2, out + ((i + 1) * w + j) * 2); 92 | } 93 | if (j + 1 < w) { 94 | cost_function = 95 | new AutoDiffCostFunction, 2, 2, 2>( 96 | new PairwiseDiff<2>{w_p}); 97 | problem.AddResidualBlock(cost_function, NULL, 98 | out + (i * w + j) * 2, out + (i * w + j + 1) * 2); 99 | } 100 | } 101 | 102 | // boundary conditions 103 | if (w > 2) { 104 | ceres::SubsetParameterization *fix0 = 105 | new ceres::SubsetParameterization(2, {0}); 106 | for (int i : {0, h - 1}) 107 | for (int j = 1; j + 1 < w; ++j) { 108 | out[(i * w + j) * 2] = i / (h - 1); 109 | problem.SetParameterization(out + (i * w + j) * 2, fix0); 110 | } 111 | } 112 | if (h > 2) { 113 | ceres::SubsetParameterization *fix1 = 114 | new ceres::SubsetParameterization(2, {1}); 115 | for (int i = 1; i + 1 < h; ++i) 116 | for (int j : {0, w - 1}) { 117 | out[(i * w + j) * 2 + 1] = j / (w - 1); 118 | problem.SetParameterization(out + (i * w + j) * 2, fix1); 119 | } 120 | } 121 | 122 | for (int i : {0, h - 1}) 123 | for (int j : {0, w - 1}) { 124 | out[(i * w + j) * 2] = i / (h - 1); 125 | out[(i * w + j) * 2 + 1] = j / (w - 1); 126 | problem.SetParameterBlockConstant(out + (i * w + j) * 2); 127 | } 128 | 129 | Solver::Options options; 130 | options.minimizer_progress_to_stdout = false; 131 | Solver::Summary summary; 132 | Solve(options, &problem, &summary); 133 | } 134 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/closest_point.h: -------------------------------------------------------------------------------- 1 | /* 2 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | */ 17 | 18 | #pragma once 19 | 20 | void closest_point(const double* target, int h, int w, double weight, double* out); 21 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/closest_point_cython.pyx: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | cdef extern from "closest_point.h": 17 | void closest_point(const double* target, int h, int w, double weight, double* output); 18 | 19 | import numpy as np 20 | cimport numpy as np 21 | cimport cython 22 | 23 | 24 | def closest_boundary_point(np.ndarray[double, ndim=3] target, double weight): 25 | cdef int h = target.shape[0] 26 | cdef int w = target.shape[1] 27 | cdef int c = target.shape[2] 28 | assert c == 2, "Last dimension must be 2, got %d" % c 29 | cdef np.ndarray[double, ndim=3, mode="c"] arr = np.ascontiguousarray(target) 30 | cdef np.ndarray[double, ndim=3, mode="c"] output = np.zeros_like(arr) 31 | closest_point(arr.data, h, w, weight, output.data) 32 | return output 33 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/interpolation.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | import numpy as np 17 | import scipy 18 | 19 | 20 | def nus_interpolate(data, sampling, shape): 21 | assert len(data.shape) == 3, \ 22 | "nus_interpolate: only support 3D data arrays, got shape %s" % list(data.shape) 23 | channels = data.shape[-1] 24 | points = sampling.reshape((-1, 2)) * (np.array(shape).T - 1) 25 | values = data.reshape((-1, channels)) 26 | xi = np.stack( 27 | np.mgrid[:shape[0], :shape[1]], 28 | axis=-1, 29 | ).reshape((-1, 2)) 30 | output_shape = tuple(shape.astype(int)) + (channels,) 31 | return scipy.interpolate.griddata( 32 | points, 33 | values, 34 | xi, 35 | ).astype(data.dtype).reshape(output_shape) 36 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/sample.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | import numpy as np 17 | 18 | 19 | def normalize_locations(locations): 20 | locations = np.copy(locations) 21 | locations[..., 0, :, 0] = 0 22 | locations[..., -1, :, 0] = 1 23 | locations[..., :, 0, 1] = 0 24 | locations[..., :, -1, 1] = 1 25 | return np.clip(locations, 0, 1) 26 | 27 | 28 | def rel_to_abs(size, locations): 29 | size = np.array(size, np.float32) 30 | return np.round(locations * (size - 1)).astype(np.int32) 31 | 32 | 33 | def sample(image, locations): 34 | return image[ 35 | locations[..., 0], 36 | locations[..., 1], 37 | : 38 | ] 39 | 40 | 41 | def sample_rel_with_proj(image, locations): 42 | return sample( 43 | image, 44 | rel_to_abs( 45 | image.shape[:2], 46 | normalize_locations(locations), 47 | ), 48 | ) 49 | -------------------------------------------------------------------------------- /research/deeplab/utils/nus/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Dmitrii Marin (https://github.com/dmitrii-marin) All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | import numpy as np 17 | import scipy 18 | from .build.closest_point_cython import closest_boundary_point as regularize_locations 19 | 20 | def get_edges(arr, ignore_label=None, target_labels=None): 21 | edges = np.zeros_like(arr) != 0 22 | 23 | edges[:,1:] = np.logical_or(edges[:,1:], arr[:,1:] != arr[:,:-1]) 24 | edges[:,:-1] = np.logical_or(edges[:,:-1], arr[:,1:] != arr[:,:-1]) 25 | 26 | edges[1:,:] = np.logical_or(edges[1:,:], arr[1:,:] != arr[:-1,:]) 27 | edges[:-1,:] = np.logical_or(edges[:-1,:], arr[1:,:] != arr[:-1,:]) 28 | 29 | if ignore_label is not None: 30 | edges[arr == ignore_label] = False 31 | 32 | if target_labels is not None: 33 | mask = np.isin(np.array(arr), target_labels) 34 | edges[np.logical_not(mask)] = False 35 | 36 | return edges 37 | 38 | 39 | def get_near_boundary_sampling_locations(edges, size, alpha=0.5): 40 | dist, closest = scipy.ndimage.morphology.distance_transform_edt( 41 | np.logical_not(edges), 42 | return_indices=True, 43 | ) 44 | closest = closest.astype(np.float) / (np.array(edges.shape[:2]) - 1)[:,None,None] 45 | closest = np.transpose(closest, [1, 2, 0]) 46 | i, j = np.mgrid[:closest.shape[0]-1:1j*size[0], :closest.shape[0]-1:1j*size[1]].astype(int) 47 | targets = closest[i, j] 48 | return regularize_locations(targets, alpha).astype(np.float32) 49 | -------------------------------------------------------------------------------- /research/deeplab/utils/save_annotation.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Saves an annotation as one png image. 16 | 17 | This script saves an annotation as one png image, and has the option to add 18 | colormap to the png image for better visualization. 19 | """ 20 | 21 | import numpy as np 22 | import PIL.Image as img 23 | import tensorflow as tf 24 | 25 | from deeplab.utils import get_dataset_colormap 26 | 27 | 28 | def save_annotation(label, 29 | save_dir, 30 | filename, 31 | add_colormap=True, 32 | normalize_to_unit_values=False, 33 | scale_values=False, 34 | colormap_type=get_dataset_colormap.get_pascal_name()): 35 | """Saves the given label to image on disk. 36 | 37 | Args: 38 | label: The numpy array to be saved. The data will be converted 39 | to uint8 and saved as png image. 40 | save_dir: String, the directory to which the results will be saved. 41 | filename: String, the image filename. 42 | add_colormap: Boolean, add color map to the label or not. 43 | normalize_to_unit_values: Boolean, normalize the input values to [0, 1]. 44 | scale_values: Boolean, scale the input values to [0, 255] for visualization. 45 | colormap_type: String, colormap type for visualization. 46 | """ 47 | # Add colormap for visualizing the prediction. 48 | if add_colormap: 49 | colored_label = get_dataset_colormap.label_to_color_image( 50 | label, colormap_type) 51 | else: 52 | colored_label = label 53 | if normalize_to_unit_values: 54 | min_value = np.amin(colored_label) 55 | max_value = np.amax(colored_label) 56 | range_value = max_value - min_value 57 | if range_value != 0: 58 | colored_label = (colored_label - min_value) / range_value 59 | 60 | if scale_values: 61 | colored_label = 255. * colored_label 62 | 63 | pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) 64 | with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: 65 | pil_image.save(f, 'PNG') 66 | -------------------------------------------------------------------------------- /research/slim/WORKSPACE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/slim/WORKSPACE -------------------------------------------------------------------------------- /research/slim/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/slim/__init__.py -------------------------------------------------------------------------------- /research/slim/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /research/slim/datasets/cifar10.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Provides data for the Cifar10 dataset. 16 | 17 | The dataset scripts used to create the dataset can be found at: 18 | tensorflow/models/research/slim/datasets/download_and_convert_cifar10.py 19 | """ 20 | 21 | from __future__ import absolute_import 22 | from __future__ import division 23 | from __future__ import print_function 24 | 25 | import os 26 | import tensorflow as tf 27 | 28 | from datasets import dataset_utils 29 | 30 | slim = tf.contrib.slim 31 | 32 | _FILE_PATTERN = 'cifar10_%s.tfrecord' 33 | 34 | SPLITS_TO_SIZES = {'train': 50000, 'test': 10000} 35 | 36 | _NUM_CLASSES = 10 37 | 38 | _ITEMS_TO_DESCRIPTIONS = { 39 | 'image': 'A [32 x 32 x 3] color image.', 40 | 'label': 'A single integer between 0 and 9', 41 | } 42 | 43 | 44 | def get_split(split_name, dataset_dir, file_pattern=None, reader=None): 45 | """Gets a dataset tuple with instructions for reading cifar10. 46 | 47 | Args: 48 | split_name: A train/test split name. 49 | dataset_dir: The base directory of the dataset sources. 50 | file_pattern: The file pattern to use when matching the dataset sources. 51 | It is assumed that the pattern contains a '%s' string so that the split 52 | name can be inserted. 53 | reader: The TensorFlow reader type. 54 | 55 | Returns: 56 | A `Dataset` namedtuple. 57 | 58 | Raises: 59 | ValueError: if `split_name` is not a valid train/test split. 60 | """ 61 | if split_name not in SPLITS_TO_SIZES: 62 | raise ValueError('split name %s was not recognized.' % split_name) 63 | 64 | if not file_pattern: 65 | file_pattern = _FILE_PATTERN 66 | file_pattern = os.path.join(dataset_dir, file_pattern % split_name) 67 | 68 | # Allowing None in the signature so that dataset_factory can use the default. 69 | if not reader: 70 | reader = tf.TFRecordReader 71 | 72 | keys_to_features = { 73 | 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 74 | 'image/format': tf.FixedLenFeature((), tf.string, default_value='png'), 75 | 'image/class/label': tf.FixedLenFeature( 76 | [], tf.int64, default_value=tf.zeros([], dtype=tf.int64)), 77 | } 78 | 79 | items_to_handlers = { 80 | 'image': slim.tfexample_decoder.Image(shape=[32, 32, 3]), 81 | 'label': slim.tfexample_decoder.Tensor('image/class/label'), 82 | } 83 | 84 | decoder = slim.tfexample_decoder.TFExampleDecoder( 85 | keys_to_features, items_to_handlers) 86 | 87 | labels_to_names = None 88 | if dataset_utils.has_labels(dataset_dir): 89 | labels_to_names = dataset_utils.read_label_file(dataset_dir) 90 | 91 | return slim.dataset.Dataset( 92 | data_sources=file_pattern, 93 | reader=reader, 94 | decoder=decoder, 95 | num_samples=SPLITS_TO_SIZES[split_name], 96 | items_to_descriptions=_ITEMS_TO_DESCRIPTIONS, 97 | num_classes=_NUM_CLASSES, 98 | labels_to_names=labels_to_names) 99 | -------------------------------------------------------------------------------- /research/slim/datasets/dataset_factory.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """A factory-pattern class which returns classification image/label pairs.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | from datasets import cifar10 22 | from datasets import flowers 23 | from datasets import imagenet 24 | from datasets import mnist 25 | from datasets import visualwakewords 26 | 27 | datasets_map = { 28 | 'cifar10': cifar10, 29 | 'flowers': flowers, 30 | 'imagenet': imagenet, 31 | 'mnist': mnist, 32 | 'visualwakewords': visualwakewords, 33 | } 34 | 35 | 36 | def get_dataset(name, split_name, dataset_dir, file_pattern=None, reader=None): 37 | """Given a dataset name and a split_name returns a Dataset. 38 | 39 | Args: 40 | name: String, the name of the dataset. 41 | split_name: A train/test split name. 42 | dataset_dir: The directory where the dataset files are stored. 43 | file_pattern: The file pattern to use for matching the dataset source files. 44 | reader: The subclass of tf.ReaderBase. If left as `None`, then the default 45 | reader defined by each dataset is used. 46 | 47 | Returns: 48 | A `Dataset` class. 49 | 50 | Raises: 51 | ValueError: If the dataset `name` is unknown. 52 | """ 53 | if name not in datasets_map: 54 | raise ValueError('Name of dataset unknown %s' % name) 55 | return datasets_map[name].get_split( 56 | split_name, 57 | dataset_dir, 58 | file_pattern, 59 | reader) 60 | -------------------------------------------------------------------------------- /research/slim/datasets/download_and_convert_imagenet.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2016 Google Inc. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | 17 | # Script to download and preprocess ImageNet Challenge 2012 18 | # training and validation data set. 19 | # 20 | # The final output of this script are sharded TFRecord files containing 21 | # serialized Example protocol buffers. See build_imagenet_data.py for 22 | # details of how the Example protocol buffers contain the ImageNet data. 23 | # 24 | # The final output of this script appears as such: 25 | # 26 | # data_dir/train-00000-of-01024 27 | # data_dir/train-00001-of-01024 28 | # ... 29 | # data_dir/train-00127-of-01024 30 | # 31 | # and 32 | # 33 | # data_dir/validation-00000-of-00128 34 | # data_dir/validation-00001-of-00128 35 | # ... 36 | # data_dir/validation-00127-of-00128 37 | # 38 | # Note that this script may take several hours to run to completion. The 39 | # conversion of the ImageNet data to TFRecords alone takes 2-3 hours depending 40 | # on the speed of your machine. Please be patient. 41 | # 42 | # **IMPORTANT** 43 | # To download the raw images, the user must create an account with image-net.org 44 | # and generate a username and access_key. The latter two are required for 45 | # downloading the raw images. 46 | # 47 | # usage: 48 | # cd research/slim 49 | # bazel build :download_and_convert_imagenet 50 | # ./bazel-bin/download_and_convert_imagenet.sh [data-dir] 51 | set -e 52 | 53 | if [ -z "$1" ]; then 54 | echo "usage download_and_convert_imagenet.sh [data dir]" 55 | exit 56 | fi 57 | 58 | # Create the output and temporary directories. 59 | DATA_DIR="${1%/}" 60 | SCRATCH_DIR="${DATA_DIR}/raw-data/" 61 | mkdir -p "${DATA_DIR}" 62 | mkdir -p "${SCRATCH_DIR}" 63 | WORK_DIR="$0.runfiles/__main__" 64 | 65 | # Download the ImageNet data. 66 | LABELS_FILE="${WORK_DIR}/datasets/imagenet_lsvrc_2015_synsets.txt" 67 | DOWNLOAD_SCRIPT="${WORK_DIR}/datasets/download_imagenet.sh" 68 | "${DOWNLOAD_SCRIPT}" "${SCRATCH_DIR}" "${LABELS_FILE}" 69 | 70 | # Note the locations of the train and validation data. 71 | TRAIN_DIRECTORY="${SCRATCH_DIR}train/" 72 | VALIDATION_DIRECTORY="${SCRATCH_DIR}validation/" 73 | 74 | # Preprocess the validation data by moving the images into the appropriate 75 | # sub-directory based on the label (synset) of the image. 76 | echo "Organizing the validation data into sub-directories." 77 | PREPROCESS_VAL_SCRIPT="${WORK_DIR}/datasets/preprocess_imagenet_validation_data.py" 78 | VAL_LABELS_FILE="${WORK_DIR}/datasets/imagenet_2012_validation_synset_labels.txt" 79 | 80 | "${PREPROCESS_VAL_SCRIPT}" "${VALIDATION_DIRECTORY}" "${VAL_LABELS_FILE}" 81 | 82 | # Convert the XML files for bounding box annotations into a single CSV. 83 | echo "Extracting bounding box information from XML." 84 | BOUNDING_BOX_SCRIPT="${WORK_DIR}/datasets/process_bounding_boxes.py" 85 | BOUNDING_BOX_FILE="${SCRATCH_DIR}/imagenet_2012_bounding_boxes.csv" 86 | BOUNDING_BOX_DIR="${SCRATCH_DIR}bounding_boxes/" 87 | 88 | "${BOUNDING_BOX_SCRIPT}" "${BOUNDING_BOX_DIR}" "${LABELS_FILE}" \ 89 | | sort >"${BOUNDING_BOX_FILE}" 90 | echo "Finished downloading and preprocessing the ImageNet data." 91 | 92 | # Build the TFRecords version of the ImageNet data. 93 | BUILD_SCRIPT="${WORK_DIR}/build_imagenet_data" 94 | OUTPUT_DIRECTORY="${DATA_DIR}" 95 | IMAGENET_METADATA_FILE="${WORK_DIR}/datasets/imagenet_metadata.txt" 96 | 97 | "${BUILD_SCRIPT}" \ 98 | --train_directory="${TRAIN_DIRECTORY}" \ 99 | --validation_directory="${VALIDATION_DIRECTORY}" \ 100 | --output_directory="${OUTPUT_DIRECTORY}" \ 101 | --imagenet_metadata_file="${IMAGENET_METADATA_FILE}" \ 102 | --labels_file="${LABELS_FILE}" \ 103 | --bounding_box_file="${BOUNDING_BOX_FILE}" 104 | -------------------------------------------------------------------------------- /research/slim/datasets/download_imagenet.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2016 Google Inc. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | 17 | # Script to download ImageNet Challenge 2012 training and validation data set. 18 | # 19 | # Downloads and decompresses raw images and bounding boxes. 20 | # 21 | # **IMPORTANT** 22 | # To download the raw images, the user must create an account with image-net.org 23 | # and generate a username and access_key. The latter two are required for 24 | # downloading the raw images. 25 | # 26 | # usage: 27 | # ./download_imagenet.sh [dirname] 28 | set -e 29 | 30 | if [ "x$IMAGENET_ACCESS_KEY" == x -o "x$IMAGENET_USERNAME" == x ]; then 31 | cat < ') 62 | sys.exit(-1) 63 | data_dir = sys.argv[1] 64 | validation_labels_file = sys.argv[2] 65 | 66 | # Read in the 50000 synsets associated with the validation data set. 67 | labels = [l.strip() for l in open(validation_labels_file).readlines()] 68 | unique_labels = set(labels) 69 | 70 | # Make all sub-directories in the validation data dir. 71 | for label in unique_labels: 72 | labeled_data_dir = os.path.join(data_dir, label) 73 | os.makedirs(labeled_data_dir) 74 | 75 | # Move all of the image to the appropriate sub-directory. 76 | for i in xrange(len(labels)): 77 | basename = 'ILSVRC2012_val_000%.5d.JPEG' % (i + 1) 78 | original_filename = os.path.join(data_dir, basename) 79 | if not os.path.exists(original_filename): 80 | print('Failed to find: ', original_filename) 81 | sys.exit(-1) 82 | new_filename = os.path.join(data_dir, labels[i], basename) 83 | os.rename(original_filename, new_filename) 84 | -------------------------------------------------------------------------------- /research/slim/deployment/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /research/slim/download_and_convert_data.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | r"""Downloads and converts a particular dataset. 16 | 17 | Usage: 18 | ```shell 19 | 20 | $ python download_and_convert_data.py \ 21 | --dataset_name=mnist \ 22 | --dataset_dir=/tmp/mnist 23 | 24 | $ python download_and_convert_data.py \ 25 | --dataset_name=cifar10 \ 26 | --dataset_dir=/tmp/cifar10 27 | 28 | $ python download_and_convert_data.py \ 29 | --dataset_name=flowers \ 30 | --dataset_dir=/tmp/flowers 31 | ``` 32 | """ 33 | from __future__ import absolute_import 34 | from __future__ import division 35 | from __future__ import print_function 36 | 37 | import tensorflow as tf 38 | 39 | from datasets import download_and_convert_cifar10 40 | from datasets import download_and_convert_flowers 41 | from datasets import download_and_convert_mnist 42 | 43 | FLAGS = tf.app.flags.FLAGS 44 | 45 | tf.app.flags.DEFINE_string( 46 | 'dataset_name', 47 | None, 48 | 'The name of the dataset to convert, one of "cifar10", "flowers", "mnist".') 49 | 50 | tf.app.flags.DEFINE_string( 51 | 'dataset_dir', 52 | None, 53 | 'The directory where the output TFRecords and temporary files are saved.') 54 | 55 | 56 | def main(_): 57 | if not FLAGS.dataset_name: 58 | raise ValueError('You must supply the dataset name with --dataset_name') 59 | if not FLAGS.dataset_dir: 60 | raise ValueError('You must supply the dataset directory with --dataset_dir') 61 | 62 | if FLAGS.dataset_name == 'cifar10': 63 | download_and_convert_cifar10.run(FLAGS.dataset_dir) 64 | elif FLAGS.dataset_name == 'flowers': 65 | download_and_convert_flowers.run(FLAGS.dataset_dir) 66 | elif FLAGS.dataset_name == 'mnist': 67 | download_and_convert_mnist.run(FLAGS.dataset_dir) 68 | else: 69 | raise ValueError( 70 | 'dataset_name [%s] was not recognized.' % FLAGS.dataset_name) 71 | 72 | if __name__ == '__main__': 73 | tf.app.run() 74 | -------------------------------------------------------------------------------- /research/slim/export_inference_graph_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for export_inference_graph.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import os 23 | 24 | 25 | import tensorflow as tf 26 | 27 | from tensorflow.python.platform import gfile 28 | import export_inference_graph 29 | 30 | 31 | class ExportInferenceGraphTest(tf.test.TestCase): 32 | 33 | def testExportInferenceGraph(self): 34 | tmpdir = self.get_temp_dir() 35 | output_file = os.path.join(tmpdir, 'inception_v3.pb') 36 | flags = tf.app.flags.FLAGS 37 | flags.output_file = output_file 38 | flags.model_name = 'inception_v3' 39 | flags.dataset_dir = tmpdir 40 | export_inference_graph.main(None) 41 | self.assertTrue(gfile.Exists(output_file)) 42 | 43 | if __name__ == '__main__': 44 | tf.test.main() 45 | -------------------------------------------------------------------------------- /research/slim/nets/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /research/slim/nets/cifarnet.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Contains a variant of the CIFAR-10 model definition.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | 23 | slim = tf.contrib.slim 24 | 25 | trunc_normal = lambda stddev: tf.truncated_normal_initializer(stddev=stddev) 26 | 27 | 28 | def cifarnet(images, num_classes=10, is_training=False, 29 | dropout_keep_prob=0.5, 30 | prediction_fn=slim.softmax, 31 | scope='CifarNet'): 32 | """Creates a variant of the CifarNet model. 33 | 34 | Note that since the output is a set of 'logits', the values fall in the 35 | interval of (-infinity, infinity). Consequently, to convert the outputs to a 36 | probability distribution over the characters, one will need to convert them 37 | using the softmax function: 38 | 39 | logits = cifarnet.cifarnet(images, is_training=False) 40 | probabilities = tf.nn.softmax(logits) 41 | predictions = tf.argmax(logits, 1) 42 | 43 | Args: 44 | images: A batch of `Tensors` of size [batch_size, height, width, channels]. 45 | num_classes: the number of classes in the dataset. If 0 or None, the logits 46 | layer is omitted and the input features to the logits layer are returned 47 | instead. 48 | is_training: specifies whether or not we're currently training the model. 49 | This variable will determine the behaviour of the dropout layer. 50 | dropout_keep_prob: the percentage of activation values that are retained. 51 | prediction_fn: a function to get predictions out of logits. 52 | scope: Optional variable_scope. 53 | 54 | Returns: 55 | net: a 2D Tensor with the logits (pre-softmax activations) if num_classes 56 | is a non-zero integer, or the input to the logits layer if num_classes 57 | is 0 or None. 58 | end_points: a dictionary from components of the network to the corresponding 59 | activation. 60 | """ 61 | end_points = {} 62 | 63 | with tf.variable_scope(scope, 'CifarNet', [images]): 64 | net = slim.conv2d(images, 64, [5, 5], scope='conv1') 65 | end_points['conv1'] = net 66 | net = slim.max_pool2d(net, [2, 2], 2, scope='pool1') 67 | end_points['pool1'] = net 68 | net = tf.nn.lrn(net, 4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm1') 69 | net = slim.conv2d(net, 64, [5, 5], scope='conv2') 70 | end_points['conv2'] = net 71 | net = tf.nn.lrn(net, 4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm2') 72 | net = slim.max_pool2d(net, [2, 2], 2, scope='pool2') 73 | end_points['pool2'] = net 74 | net = slim.flatten(net) 75 | end_points['Flatten'] = net 76 | net = slim.fully_connected(net, 384, scope='fc3') 77 | end_points['fc3'] = net 78 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 79 | scope='dropout3') 80 | net = slim.fully_connected(net, 192, scope='fc4') 81 | end_points['fc4'] = net 82 | if not num_classes: 83 | return net, end_points 84 | logits = slim.fully_connected(net, num_classes, 85 | biases_initializer=tf.zeros_initializer(), 86 | weights_initializer=trunc_normal(1/192.0), 87 | weights_regularizer=None, 88 | activation_fn=None, 89 | scope='logits') 90 | 91 | end_points['Logits'] = logits 92 | end_points['Predictions'] = prediction_fn(logits, scope='Predictions') 93 | 94 | return logits, end_points 95 | cifarnet.default_image_size = 32 96 | 97 | 98 | def cifarnet_arg_scope(weight_decay=0.004): 99 | """Defines the default cifarnet argument scope. 100 | 101 | Args: 102 | weight_decay: The weight decay to use for regularizing the model. 103 | 104 | Returns: 105 | An `arg_scope` to use for the inception v3 model. 106 | """ 107 | with slim.arg_scope( 108 | [slim.conv2d], 109 | weights_initializer=tf.truncated_normal_initializer(stddev=5e-2), 110 | activation_fn=tf.nn.relu): 111 | with slim.arg_scope( 112 | [slim.fully_connected], 113 | biases_initializer=tf.constant_initializer(0.1), 114 | weights_initializer=trunc_normal(0.04), 115 | weights_regularizer=slim.l2_regularizer(weight_decay), 116 | activation_fn=tf.nn.relu) as sc: 117 | return sc 118 | -------------------------------------------------------------------------------- /research/slim/nets/cyclegan_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for tensorflow.contrib.slim.nets.cyclegan.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | 23 | from nets import cyclegan 24 | 25 | 26 | # TODO(joelshor): Add a test to check generator endpoints. 27 | class CycleganTest(tf.test.TestCase): 28 | 29 | def test_generator_inference(self): 30 | """Check one inference step.""" 31 | img_batch = tf.zeros([2, 32, 32, 3]) 32 | model_output, _ = cyclegan.cyclegan_generator_resnet(img_batch) 33 | with self.test_session() as sess: 34 | sess.run(tf.global_variables_initializer()) 35 | sess.run(model_output) 36 | 37 | def _test_generator_graph_helper(self, shape): 38 | """Check that generator can take small and non-square inputs.""" 39 | output_imgs, _ = cyclegan.cyclegan_generator_resnet(tf.ones(shape)) 40 | self.assertAllEqual(shape, output_imgs.shape.as_list()) 41 | 42 | def test_generator_graph_small(self): 43 | self._test_generator_graph_helper([4, 32, 32, 3]) 44 | 45 | def test_generator_graph_medium(self): 46 | self._test_generator_graph_helper([3, 128, 128, 3]) 47 | 48 | def test_generator_graph_nonsquare(self): 49 | self._test_generator_graph_helper([2, 80, 400, 3]) 50 | 51 | def test_generator_unknown_batch_dim(self): 52 | """Check that generator can take unknown batch dimension inputs.""" 53 | img = tf.placeholder(tf.float32, shape=[None, 32, None, 3]) 54 | output_imgs, _ = cyclegan.cyclegan_generator_resnet(img) 55 | 56 | self.assertAllEqual([None, 32, None, 3], output_imgs.shape.as_list()) 57 | 58 | def _input_and_output_same_shape_helper(self, kernel_size): 59 | img_batch = tf.placeholder(tf.float32, shape=[None, 32, 32, 3]) 60 | output_img_batch, _ = cyclegan.cyclegan_generator_resnet( 61 | img_batch, kernel_size=kernel_size) 62 | 63 | self.assertAllEqual(img_batch.shape.as_list(), 64 | output_img_batch.shape.as_list()) 65 | 66 | def input_and_output_same_shape_kernel3(self): 67 | self._input_and_output_same_shape_helper(3) 68 | 69 | def input_and_output_same_shape_kernel4(self): 70 | self._input_and_output_same_shape_helper(4) 71 | 72 | def input_and_output_same_shape_kernel5(self): 73 | self._input_and_output_same_shape_helper(5) 74 | 75 | def input_and_output_same_shape_kernel6(self): 76 | self._input_and_output_same_shape_helper(6) 77 | 78 | def _error_if_height_not_multiple_of_four_helper(self, height): 79 | self.assertRaisesRegexp( 80 | ValueError, 81 | 'The input height must be a multiple of 4.', 82 | cyclegan.cyclegan_generator_resnet, 83 | tf.placeholder(tf.float32, shape=[None, height, 32, 3])) 84 | 85 | def test_error_if_height_not_multiple_of_four_height29(self): 86 | self._error_if_height_not_multiple_of_four_helper(29) 87 | 88 | def test_error_if_height_not_multiple_of_four_height30(self): 89 | self._error_if_height_not_multiple_of_four_helper(30) 90 | 91 | def test_error_if_height_not_multiple_of_four_height31(self): 92 | self._error_if_height_not_multiple_of_four_helper(31) 93 | 94 | def _error_if_width_not_multiple_of_four_helper(self, width): 95 | self.assertRaisesRegexp( 96 | ValueError, 97 | 'The input width must be a multiple of 4.', 98 | cyclegan.cyclegan_generator_resnet, 99 | tf.placeholder(tf.float32, shape=[None, 32, width, 3])) 100 | 101 | def test_error_if_width_not_multiple_of_four_width29(self): 102 | self._error_if_width_not_multiple_of_four_helper(29) 103 | 104 | def test_error_if_width_not_multiple_of_four_width30(self): 105 | self._error_if_width_not_multiple_of_four_helper(30) 106 | 107 | def test_error_if_width_not_multiple_of_four_width31(self): 108 | self._error_if_width_not_multiple_of_four_helper(31) 109 | 110 | 111 | if __name__ == '__main__': 112 | tf.test.main() 113 | -------------------------------------------------------------------------------- /research/slim/nets/dcgan_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for dcgan.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | from six.moves import xrange # pylint: disable=redefined-builtin 22 | import tensorflow as tf 23 | 24 | from nets import dcgan 25 | 26 | 27 | class DCGANTest(tf.test.TestCase): 28 | 29 | def test_generator_run(self): 30 | tf.set_random_seed(1234) 31 | noise = tf.random_normal([100, 64]) 32 | image, _ = dcgan.generator(noise) 33 | with self.test_session() as sess: 34 | sess.run(tf.global_variables_initializer()) 35 | image.eval() 36 | 37 | def test_generator_graph(self): 38 | tf.set_random_seed(1234) 39 | # Check graph construction for a number of image size/depths and batch 40 | # sizes. 41 | for i, batch_size in zip(xrange(3, 7), xrange(3, 8)): 42 | tf.reset_default_graph() 43 | final_size = 2 ** i 44 | noise = tf.random_normal([batch_size, 64]) 45 | image, end_points = dcgan.generator( 46 | noise, 47 | depth=32, 48 | final_size=final_size) 49 | 50 | self.assertAllEqual([batch_size, final_size, final_size, 3], 51 | image.shape.as_list()) 52 | 53 | expected_names = ['deconv%i' % j for j in xrange(1, i)] + ['logits'] 54 | self.assertSetEqual(set(expected_names), set(end_points.keys())) 55 | 56 | # Check layer depths. 57 | for j in range(1, i): 58 | layer = end_points['deconv%i' % j] 59 | self.assertEqual(32 * 2**(i-j-1), layer.get_shape().as_list()[-1]) 60 | 61 | def test_generator_invalid_input(self): 62 | wrong_dim_input = tf.zeros([5, 32, 32]) 63 | with self.assertRaises(ValueError): 64 | dcgan.generator(wrong_dim_input) 65 | 66 | correct_input = tf.zeros([3, 2]) 67 | with self.assertRaisesRegexp(ValueError, 'must be a power of 2'): 68 | dcgan.generator(correct_input, final_size=30) 69 | 70 | with self.assertRaisesRegexp(ValueError, 'must be greater than 8'): 71 | dcgan.generator(correct_input, final_size=4) 72 | 73 | def test_discriminator_run(self): 74 | image = tf.random_uniform([5, 32, 32, 3], -1, 1) 75 | output, _ = dcgan.discriminator(image) 76 | with self.test_session() as sess: 77 | sess.run(tf.global_variables_initializer()) 78 | output.eval() 79 | 80 | def test_discriminator_graph(self): 81 | # Check graph construction for a number of image size/depths and batch 82 | # sizes. 83 | for i, batch_size in zip(xrange(1, 6), xrange(3, 8)): 84 | tf.reset_default_graph() 85 | img_w = 2 ** i 86 | image = tf.random_uniform([batch_size, img_w, img_w, 3], -1, 1) 87 | output, end_points = dcgan.discriminator( 88 | image, 89 | depth=32) 90 | 91 | self.assertAllEqual([batch_size, 1], output.get_shape().as_list()) 92 | 93 | expected_names = ['conv%i' % j for j in xrange(1, i+1)] + ['logits'] 94 | self.assertSetEqual(set(expected_names), set(end_points.keys())) 95 | 96 | # Check layer depths. 97 | for j in range(1, i+1): 98 | layer = end_points['conv%i' % j] 99 | self.assertEqual(32 * 2**(j-1), layer.get_shape().as_list()[-1]) 100 | 101 | def test_discriminator_invalid_input(self): 102 | wrong_dim_img = tf.zeros([5, 32, 32]) 103 | with self.assertRaises(ValueError): 104 | dcgan.discriminator(wrong_dim_img) 105 | 106 | spatially_undefined_shape = tf.placeholder(tf.float32, [5, 32, None, 3]) 107 | with self.assertRaises(ValueError): 108 | dcgan.discriminator(spatially_undefined_shape) 109 | 110 | not_square = tf.zeros([5, 32, 16, 3]) 111 | with self.assertRaisesRegexp(ValueError, 'not have equal width and height'): 112 | dcgan.discriminator(not_square) 113 | 114 | not_power_2 = tf.zeros([5, 30, 30, 3]) 115 | with self.assertRaisesRegexp(ValueError, 'not a power of 2'): 116 | dcgan.discriminator(not_power_2) 117 | 118 | 119 | if __name__ == '__main__': 120 | tf.test.main() 121 | -------------------------------------------------------------------------------- /research/slim/nets/inception.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Brings all inception models under one namespace.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | # pylint: disable=unused-import 22 | from nets.inception_resnet_v2 import inception_resnet_v2 23 | from nets.inception_resnet_v2 import inception_resnet_v2_arg_scope 24 | from nets.inception_resnet_v2 import inception_resnet_v2_base 25 | from nets.inception_v1 import inception_v1 26 | from nets.inception_v1 import inception_v1_arg_scope 27 | from nets.inception_v1 import inception_v1_base 28 | from nets.inception_v2 import inception_v2 29 | from nets.inception_v2 import inception_v2_arg_scope 30 | from nets.inception_v2 import inception_v2_base 31 | from nets.inception_v3 import inception_v3 32 | from nets.inception_v3 import inception_v3_arg_scope 33 | from nets.inception_v3 import inception_v3_base 34 | from nets.inception_v4 import inception_v4 35 | from nets.inception_v4 import inception_v4_arg_scope 36 | from nets.inception_v4 import inception_v4_base 37 | # pylint: enable=unused-import 38 | -------------------------------------------------------------------------------- /research/slim/nets/inception_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Contains common code shared by all inception models. 16 | 17 | Usage of arg scope: 18 | with slim.arg_scope(inception_arg_scope()): 19 | logits, end_points = inception.inception_v3(images, num_classes, 20 | is_training=is_training) 21 | 22 | """ 23 | from __future__ import absolute_import 24 | from __future__ import division 25 | from __future__ import print_function 26 | 27 | import tensorflow as tf 28 | 29 | slim = tf.contrib.slim 30 | 31 | 32 | def inception_arg_scope(weight_decay=0.00004, 33 | use_batch_norm=True, 34 | batch_norm_decay=0.9997, 35 | batch_norm_epsilon=0.001, 36 | activation_fn=tf.nn.relu, 37 | batch_norm_updates_collections=tf.GraphKeys.UPDATE_OPS, 38 | batch_norm_scale=False): 39 | """Defines the default arg scope for inception models. 40 | 41 | Args: 42 | weight_decay: The weight decay to use for regularizing the model. 43 | use_batch_norm: "If `True`, batch_norm is applied after each convolution. 44 | batch_norm_decay: Decay for batch norm moving average. 45 | batch_norm_epsilon: Small float added to variance to avoid dividing by zero 46 | in batch norm. 47 | activation_fn: Activation function for conv2d. 48 | batch_norm_updates_collections: Collection for the update ops for 49 | batch norm. 50 | batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the 51 | activations in the batch normalization layer. 52 | 53 | Returns: 54 | An `arg_scope` to use for the inception models. 55 | """ 56 | batch_norm_params = { 57 | # Decay for the moving averages. 58 | 'decay': batch_norm_decay, 59 | # epsilon to prevent 0s in variance. 60 | 'epsilon': batch_norm_epsilon, 61 | # collection containing update_ops. 62 | 'updates_collections': batch_norm_updates_collections, 63 | # use fused batch norm if possible. 64 | 'fused': None, 65 | 'scale': batch_norm_scale, 66 | } 67 | if use_batch_norm: 68 | normalizer_fn = slim.batch_norm 69 | normalizer_params = batch_norm_params 70 | else: 71 | normalizer_fn = None 72 | normalizer_params = {} 73 | # Set weight_decay for weights in Conv and FC layers. 74 | with slim.arg_scope([slim.conv2d, slim.fully_connected], 75 | weights_regularizer=slim.l2_regularizer(weight_decay)): 76 | with slim.arg_scope( 77 | [slim.conv2d], 78 | weights_initializer=slim.variance_scaling_initializer(), 79 | activation_fn=activation_fn, 80 | normalizer_fn=normalizer_fn, 81 | normalizer_params=normalizer_params) as sc: 82 | return sc 83 | -------------------------------------------------------------------------------- /research/slim/nets/lenet.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Contains a variant of the LeNet model definition.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | 23 | slim = tf.contrib.slim 24 | 25 | 26 | def lenet(images, num_classes=10, is_training=False, 27 | dropout_keep_prob=0.5, 28 | prediction_fn=slim.softmax, 29 | scope='LeNet'): 30 | """Creates a variant of the LeNet model. 31 | 32 | Note that since the output is a set of 'logits', the values fall in the 33 | interval of (-infinity, infinity). Consequently, to convert the outputs to a 34 | probability distribution over the characters, one will need to convert them 35 | using the softmax function: 36 | 37 | logits = lenet.lenet(images, is_training=False) 38 | probabilities = tf.nn.softmax(logits) 39 | predictions = tf.argmax(logits, 1) 40 | 41 | Args: 42 | images: A batch of `Tensors` of size [batch_size, height, width, channels]. 43 | num_classes: the number of classes in the dataset. If 0 or None, the logits 44 | layer is omitted and the input features to the logits layer are returned 45 | instead. 46 | is_training: specifies whether or not we're currently training the model. 47 | This variable will determine the behaviour of the dropout layer. 48 | dropout_keep_prob: the percentage of activation values that are retained. 49 | prediction_fn: a function to get predictions out of logits. 50 | scope: Optional variable_scope. 51 | 52 | Returns: 53 | net: a 2D Tensor with the logits (pre-softmax activations) if num_classes 54 | is a non-zero integer, or the inon-dropped-out nput to the logits layer 55 | if num_classes is 0 or None. 56 | end_points: a dictionary from components of the network to the corresponding 57 | activation. 58 | """ 59 | end_points = {} 60 | 61 | with tf.variable_scope(scope, 'LeNet', [images]): 62 | net = end_points['conv1'] = slim.conv2d(images, 32, [5, 5], scope='conv1') 63 | net = end_points['pool1'] = slim.max_pool2d(net, [2, 2], 2, scope='pool1') 64 | net = end_points['conv2'] = slim.conv2d(net, 64, [5, 5], scope='conv2') 65 | net = end_points['pool2'] = slim.max_pool2d(net, [2, 2], 2, scope='pool2') 66 | net = slim.flatten(net) 67 | end_points['Flatten'] = net 68 | 69 | net = end_points['fc3'] = slim.fully_connected(net, 1024, scope='fc3') 70 | if not num_classes: 71 | return net, end_points 72 | net = end_points['dropout3'] = slim.dropout( 73 | net, dropout_keep_prob, is_training=is_training, scope='dropout3') 74 | logits = end_points['Logits'] = slim.fully_connected( 75 | net, num_classes, activation_fn=None, scope='fc4') 76 | 77 | end_points['Predictions'] = prediction_fn(logits, scope='Predictions') 78 | 79 | return logits, end_points 80 | lenet.default_image_size = 28 81 | 82 | 83 | def lenet_arg_scope(weight_decay=0.0): 84 | """Defines the default lenet argument scope. 85 | 86 | Args: 87 | weight_decay: The weight decay to use for regularizing the model. 88 | 89 | Returns: 90 | An `arg_scope` to use for the inception v3 model. 91 | """ 92 | with slim.arg_scope( 93 | [slim.conv2d, slim.fully_connected], 94 | weights_regularizer=slim.l2_regularizer(weight_decay), 95 | weights_initializer=tf.truncated_normal_initializer(stddev=0.1), 96 | activation_fn=tf.nn.relu) as sc: 97 | return sc 98 | -------------------------------------------------------------------------------- /research/slim/nets/mobilenet/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/slim/nets/mobilenet/__init__.py -------------------------------------------------------------------------------- /research/slim/nets/mobilenet/madds_top1_accuracy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/slim/nets/mobilenet/madds_top1_accuracy.png -------------------------------------------------------------------------------- /research/slim/nets/mobilenet/mnet_v1_vs_v2_pixel1_latency.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/slim/nets/mobilenet/mnet_v1_vs_v2_pixel1_latency.png -------------------------------------------------------------------------------- /research/slim/nets/mobilenet_v1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmitrii-marin/adaptive-sampling/95fbcc1eea751be1166f2400bceafd2c619b8638/research/slim/nets/mobilenet_v1.png -------------------------------------------------------------------------------- /research/slim/nets/nasnet/README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow-Slim NASNet-A Implementation/Checkpoints 2 | This directory contains the code for the NASNet-A model from the paper 3 | [Learning Transferable Architectures for Scalable Image Recognition](https://arxiv.org/abs/1707.07012) by Zoph et al. 4 | In nasnet.py there are three different configurations of NASNet-A that are implementented. One of the models is the NASNet-A built for CIFAR-10 and the 5 | other two are variants of NASNet-A trained on ImageNet, which are listed below. 6 | 7 | # Pre-Trained Models 8 | Two NASNet-A checkpoints are available that have been trained on the 9 | [ILSVRC-2012-CLS](http://www.image-net.org/challenges/LSVRC/2012/) 10 | image classification dataset. Accuracies were computed by evaluating using a single image crop. 11 | 12 | Model Checkpoint | Million MACs | Million Parameters | Top-1 Accuracy| Top-5 Accuracy | 13 | :----:|:------------:|:----------:|:-------:|:-------:| 14 | [NASNet-A_Mobile_224](https://storage.googleapis.com/download.tensorflow.org/models/nasnet-a_mobile_04_10_2017.tar.gz)|564|5.3|74.0|91.6| 15 | [NASNet-A_Large_331](https://storage.googleapis.com/download.tensorflow.org/models/nasnet-a_large_04_10_2017.tar.gz)|23800|88.9|82.7|96.2| 16 | 17 | 18 | Here is an example of how to download the NASNet-A_Mobile_224 checkpoint. The way to download the NASNet-A_Large_331 is the same. 19 | 20 | ```shell 21 | CHECKPOINT_DIR=/tmp/checkpoints 22 | mkdir ${CHECKPOINT_DIR} 23 | cd ${CHECKPOINT_DIR} 24 | wget https://storage.googleapis.com/download.tensorflow.org/models/nasnet-a_mobile_04_10_2017.tar.gz 25 | tar -xvf nasnet-a_mobile_04_10_2017.tar.gz 26 | rm nasnet-a_mobile_04_10_2017.tar.gz 27 | ``` 28 | More information on integrating NASNet Models into your project can be found at the [TF-Slim Image Classification Library](https://github.com/tensorflow/models/blob/master/research/slim/README.md). 29 | 30 | To get started running models on-device go to [TensorFlow Mobile](https://www.tensorflow.org/mobile/). 31 | 32 | ## Sample Commands for using NASNet-A Mobile and Large Checkpoints for Inference 33 | ------- 34 | Run eval with the NASNet-A mobile ImageNet model 35 | 36 | ```shell 37 | DATASET_DIR=/tmp/imagenet 38 | EVAL_DIR=/tmp/tfmodel/eval 39 | CHECKPOINT_DIR=/tmp/checkpoints/model.ckpt 40 | python tensorflow_models/research/slim/eval_image_classifier \ 41 | --checkpoint_path=${CHECKPOINT_DIR} \ 42 | --eval_dir=${EVAL_DIR} \ 43 | --dataset_dir=${DATASET_DIR} \ 44 | --dataset_name=imagenet \ 45 | --dataset_split_name=validation \ 46 | --model_name=nasnet_mobile \ 47 | --eval_image_size=224 48 | ``` 49 | 50 | Run eval with the NASNet-A large ImageNet model 51 | 52 | ```shell 53 | DATASET_DIR=/tmp/imagenet 54 | EVAL_DIR=/tmp/tfmodel/eval 55 | CHECKPOINT_DIR=/tmp/checkpoints/model.ckpt 56 | python tensorflow_models/research/slim/eval_image_classifier \ 57 | --checkpoint_path=${CHECKPOINT_DIR} \ 58 | --eval_dir=${EVAL_DIR} \ 59 | --dataset_dir=${DATASET_DIR} \ 60 | --dataset_name=imagenet \ 61 | --dataset_split_name=validation \ 62 | --model_name=nasnet_large \ 63 | --eval_image_size=331 64 | ``` 65 | -------------------------------------------------------------------------------- /research/slim/nets/nasnet/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /research/slim/nets/nasnet/nasnet_utils_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for slim.nets.nasnet.nasnet_utils.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | 23 | from nets.nasnet import nasnet_utils 24 | 25 | 26 | class NasnetUtilsTest(tf.test.TestCase): 27 | 28 | def testCalcReductionLayers(self): 29 | num_cells = 18 30 | num_reduction_layers = 2 31 | reduction_layers = nasnet_utils.calc_reduction_layers( 32 | num_cells, num_reduction_layers) 33 | self.assertEqual(len(reduction_layers), 2) 34 | self.assertEqual(reduction_layers[0], 6) 35 | self.assertEqual(reduction_layers[1], 12) 36 | 37 | def testGetChannelIndex(self): 38 | data_formats = ['NHWC', 'NCHW'] 39 | for data_format in data_formats: 40 | index = nasnet_utils.get_channel_index(data_format) 41 | correct_index = 3 if data_format == 'NHWC' else 1 42 | self.assertEqual(index, correct_index) 43 | 44 | def testGetChannelDim(self): 45 | data_formats = ['NHWC', 'NCHW'] 46 | shape = [10, 20, 30, 40] 47 | for data_format in data_formats: 48 | dim = nasnet_utils.get_channel_dim(shape, data_format) 49 | correct_dim = shape[3] if data_format == 'NHWC' else shape[1] 50 | self.assertEqual(dim, correct_dim) 51 | 52 | def testGlobalAvgPool(self): 53 | data_formats = ['NHWC', 'NCHW'] 54 | inputs = tf.placeholder(tf.float32, (5, 10, 20, 10)) 55 | for data_format in data_formats: 56 | output = nasnet_utils.global_avg_pool( 57 | inputs, data_format) 58 | self.assertEqual(output.shape, [5, 10]) 59 | 60 | 61 | if __name__ == '__main__': 62 | tf.test.main() 63 | -------------------------------------------------------------------------------- /research/slim/nets/nets_factory_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 Google Inc. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for slim.inception.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | 23 | import tensorflow as tf 24 | 25 | from nets import nets_factory 26 | 27 | 28 | class NetworksTest(tf.test.TestCase): 29 | 30 | def testGetNetworkFnFirstHalf(self): 31 | batch_size = 5 32 | num_classes = 1000 33 | for net in list(nets_factory.networks_map.keys())[:10]: 34 | with tf.Graph().as_default() as g, self.test_session(g): 35 | net_fn = nets_factory.get_network_fn(net, num_classes=num_classes) 36 | # Most networks use 224 as their default_image_size 37 | image_size = getattr(net_fn, 'default_image_size', 224) 38 | if net not in ['i3d', 's3dg']: 39 | inputs = tf.random_uniform( 40 | (batch_size, image_size, image_size, 3)) 41 | logits, end_points = net_fn(inputs) 42 | self.assertTrue(isinstance(logits, tf.Tensor)) 43 | self.assertTrue(isinstance(end_points, dict)) 44 | self.assertEqual(logits.get_shape().as_list()[0], batch_size) 45 | self.assertEqual(logits.get_shape().as_list()[-1], num_classes) 46 | 47 | def testGetNetworkFnSecondHalf(self): 48 | batch_size = 5 49 | num_classes = 1000 50 | for net in list(nets_factory.networks_map.keys())[10:]: 51 | with tf.Graph().as_default() as g, self.test_session(g): 52 | net_fn = nets_factory.get_network_fn(net, num_classes=num_classes) 53 | # Most networks use 224 as their default_image_size 54 | image_size = getattr(net_fn, 'default_image_size', 224) 55 | if net not in ['i3d', 's3dg']: 56 | inputs = tf.random_uniform( 57 | (batch_size, image_size, image_size, 3)) 58 | logits, end_points = net_fn(inputs) 59 | self.assertTrue(isinstance(logits, tf.Tensor)) 60 | self.assertTrue(isinstance(end_points, dict)) 61 | self.assertEqual(logits.get_shape().as_list()[0], batch_size) 62 | self.assertEqual(logits.get_shape().as_list()[-1], num_classes) 63 | 64 | def testGetNetworkFnVideoModels(self): 65 | batch_size = 5 66 | num_classes = 400 67 | for net in ['i3d', 's3dg']: 68 | with tf.Graph().as_default() as g, self.test_session(g): 69 | net_fn = nets_factory.get_network_fn(net, num_classes=num_classes) 70 | # Most networks use 224 as their default_image_size 71 | image_size = getattr(net_fn, 'default_image_size', 224) // 2 72 | inputs = tf.random_uniform( 73 | (batch_size, 10, image_size, image_size, 3)) 74 | logits, end_points = net_fn(inputs) 75 | self.assertTrue(isinstance(logits, tf.Tensor)) 76 | self.assertTrue(isinstance(end_points, dict)) 77 | self.assertEqual(logits.get_shape().as_list()[0], batch_size) 78 | self.assertEqual(logits.get_shape().as_list()[-1], num_classes) 79 | 80 | if __name__ == '__main__': 81 | tf.test.main() 82 | -------------------------------------------------------------------------------- /research/slim/preprocessing/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /research/slim/preprocessing/lenet_preprocessing.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Provides utilities for preprocessing.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | 23 | slim = tf.contrib.slim 24 | 25 | 26 | def preprocess_image(image, output_height, output_width, is_training): 27 | """Preprocesses the given image. 28 | 29 | Args: 30 | image: A `Tensor` representing an image of arbitrary size. 31 | output_height: The height of the image after preprocessing. 32 | output_width: The width of the image after preprocessing. 33 | is_training: `True` if we're preprocessing the image for training and 34 | `False` otherwise. 35 | 36 | Returns: 37 | A preprocessed image. 38 | """ 39 | image = tf.to_float(image) 40 | image = tf.image.resize_image_with_crop_or_pad( 41 | image, output_width, output_height) 42 | image = tf.subtract(image, 128.0) 43 | image = tf.div(image, 128.0) 44 | return image 45 | -------------------------------------------------------------------------------- /research/slim/preprocessing/preprocessing_factory.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Contains a factory for building various models.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | 23 | from preprocessing import cifarnet_preprocessing 24 | from preprocessing import inception_preprocessing 25 | from preprocessing import lenet_preprocessing 26 | from preprocessing import vgg_preprocessing 27 | 28 | slim = tf.contrib.slim 29 | 30 | 31 | def get_preprocessing(name, is_training=False): 32 | """Returns preprocessing_fn(image, height, width, **kwargs). 33 | 34 | Args: 35 | name: The name of the preprocessing function. 36 | is_training: `True` if the model is being used for training and `False` 37 | otherwise. 38 | 39 | Returns: 40 | preprocessing_fn: A function that preprocessing a single image (pre-batch). 41 | It has the following signature: 42 | image = preprocessing_fn(image, output_height, output_width, ...). 43 | 44 | Raises: 45 | ValueError: If Preprocessing `name` is not recognized. 46 | """ 47 | preprocessing_fn_map = { 48 | 'cifarnet': cifarnet_preprocessing, 49 | 'inception': inception_preprocessing, 50 | 'inception_v1': inception_preprocessing, 51 | 'inception_v2': inception_preprocessing, 52 | 'inception_v3': inception_preprocessing, 53 | 'inception_v4': inception_preprocessing, 54 | 'inception_resnet_v2': inception_preprocessing, 55 | 'lenet': lenet_preprocessing, 56 | 'mobilenet_v1': inception_preprocessing, 57 | 'mobilenet_v2': inception_preprocessing, 58 | 'mobilenet_v2_035': inception_preprocessing, 59 | 'mobilenet_v2_140': inception_preprocessing, 60 | 'nasnet_mobile': inception_preprocessing, 61 | 'nasnet_large': inception_preprocessing, 62 | 'pnasnet_mobile': inception_preprocessing, 63 | 'pnasnet_large': inception_preprocessing, 64 | 'resnet_v1_50': vgg_preprocessing, 65 | 'resnet_v1_101': vgg_preprocessing, 66 | 'resnet_v1_152': vgg_preprocessing, 67 | 'resnet_v1_200': vgg_preprocessing, 68 | 'resnet_v2_50': vgg_preprocessing, 69 | 'resnet_v2_101': vgg_preprocessing, 70 | 'resnet_v2_152': vgg_preprocessing, 71 | 'resnet_v2_200': vgg_preprocessing, 72 | 'vgg': vgg_preprocessing, 73 | 'vgg_a': vgg_preprocessing, 74 | 'vgg_16': vgg_preprocessing, 75 | 'vgg_19': vgg_preprocessing, 76 | } 77 | 78 | if name not in preprocessing_fn_map: 79 | raise ValueError('Preprocessing name [%s] was not recognized' % name) 80 | 81 | def preprocessing_fn(image, output_height, output_width, **kwargs): 82 | return preprocessing_fn_map[name].preprocess_image( 83 | image, output_height, output_width, is_training=is_training, **kwargs) 84 | 85 | return preprocessing_fn 86 | -------------------------------------------------------------------------------- /research/slim/scripts/finetune_inception_resnet_v2_on_flowers.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # This script performs the following operations: 18 | # 1. Downloads the Flowers dataset 19 | # 2. Fine-tunes an Inception Resnet V2 model on the Flowers training set. 20 | # 3. Evaluates the model on the Flowers validation set. 21 | # 22 | # Usage: 23 | # cd slim 24 | # ./slim/scripts/finetune_inception_resnet_v2_on_flowers.sh 25 | set -e 26 | 27 | # Where the pre-trained Inception Resnet V2 checkpoint is saved to. 28 | PRETRAINED_CHECKPOINT_DIR=/tmp/checkpoints 29 | 30 | # Where the pre-trained Inception Resnet V2 checkpoint is saved to. 31 | MODEL_NAME=inception_resnet_v2 32 | 33 | # Where the training (fine-tuned) checkpoint and logs will be saved to. 34 | TRAIN_DIR=/tmp/flowers-models/${MODEL_NAME} 35 | 36 | # Where the dataset is saved to. 37 | DATASET_DIR=/tmp/flowers 38 | 39 | # Download the pre-trained checkpoint. 40 | if [ ! -d "$PRETRAINED_CHECKPOINT_DIR" ]; then 41 | mkdir ${PRETRAINED_CHECKPOINT_DIR} 42 | fi 43 | if [ ! -f ${PRETRAINED_CHECKPOINT_DIR}/${MODEL_NAME}.ckpt ]; then 44 | wget http://download.tensorflow.org/models/inception_resnet_v2_2016_08_30.tar.gz 45 | tar -xvf inception_resnet_v2_2016_08_30.tar.gz 46 | mv inception_resnet_v2.ckpt ${PRETRAINED_CHECKPOINT_DIR}/${MODEL_NAME}.ckpt 47 | rm inception_resnet_v2_2016_08_30.tar.gz 48 | fi 49 | 50 | # Download the dataset 51 | python download_and_convert_data.py \ 52 | --dataset_name=flowers \ 53 | --dataset_dir=${DATASET_DIR} 54 | 55 | # Fine-tune only the new layers for 1000 steps. 56 | python train_image_classifier.py \ 57 | --train_dir=${TRAIN_DIR} \ 58 | --dataset_name=flowers \ 59 | --dataset_split_name=train \ 60 | --dataset_dir=${DATASET_DIR} \ 61 | --model_name=${MODEL_NAME} \ 62 | --checkpoint_path=${PRETRAINED_CHECKPOINT_DIR}/${MODEL_NAME}.ckpt \ 63 | --checkpoint_exclude_scopes=InceptionResnetV2/Logits,InceptionResnetV2/AuxLogits \ 64 | --trainable_scopes=InceptionResnetV2/Logits,InceptionResnetV2/AuxLogits \ 65 | --max_number_of_steps=1000 \ 66 | --batch_size=32 \ 67 | --learning_rate=0.01 \ 68 | --learning_rate_decay_type=fixed \ 69 | --save_interval_secs=60 \ 70 | --save_summaries_secs=60 \ 71 | --log_every_n_steps=10 \ 72 | --optimizer=rmsprop \ 73 | --weight_decay=0.00004 74 | 75 | # Run evaluation. 76 | python eval_image_classifier.py \ 77 | --checkpoint_path=${TRAIN_DIR} \ 78 | --eval_dir=${TRAIN_DIR} \ 79 | --dataset_name=flowers \ 80 | --dataset_split_name=validation \ 81 | --dataset_dir=${DATASET_DIR} \ 82 | --model_name=${MODEL_NAME} 83 | 84 | # Fine-tune all the new layers for 500 steps. 85 | python train_image_classifier.py \ 86 | --train_dir=${TRAIN_DIR}/all \ 87 | --dataset_name=flowers \ 88 | --dataset_split_name=train \ 89 | --dataset_dir=${DATASET_DIR} \ 90 | --model_name=${MODEL_NAME} \ 91 | --checkpoint_path=${TRAIN_DIR} \ 92 | --max_number_of_steps=500 \ 93 | --batch_size=32 \ 94 | --learning_rate=0.0001 \ 95 | --learning_rate_decay_type=fixed \ 96 | --save_interval_secs=60 \ 97 | --save_summaries_secs=60 \ 98 | --log_every_n_steps=10 \ 99 | --optimizer=rmsprop \ 100 | --weight_decay=0.00004 101 | 102 | # Run evaluation. 103 | python eval_image_classifier.py \ 104 | --checkpoint_path=${TRAIN_DIR}/all \ 105 | --eval_dir=${TRAIN_DIR}/all \ 106 | --dataset_name=flowers \ 107 | --dataset_split_name=validation \ 108 | --dataset_dir=${DATASET_DIR} \ 109 | --model_name=${MODEL_NAME} 110 | -------------------------------------------------------------------------------- /research/slim/scripts/finetune_inception_v1_on_flowers.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # This script performs the following operations: 18 | # 1. Downloads the Flowers dataset 19 | # 2. Fine-tunes an InceptionV1 model on the Flowers training set. 20 | # 3. Evaluates the model on the Flowers validation set. 21 | # 22 | # Usage: 23 | # cd slim 24 | # ./slim/scripts/finetune_inception_v1_on_flowers.sh 25 | set -e 26 | 27 | # Where the pre-trained InceptionV1 checkpoint is saved to. 28 | PRETRAINED_CHECKPOINT_DIR=/tmp/checkpoints 29 | 30 | # Where the training (fine-tuned) checkpoint and logs will be saved to. 31 | TRAIN_DIR=/tmp/flowers-models/inception_v1 32 | 33 | # Where the dataset is saved to. 34 | DATASET_DIR=/tmp/flowers 35 | 36 | # Download the pre-trained checkpoint. 37 | if [ ! -d "$PRETRAINED_CHECKPOINT_DIR" ]; then 38 | mkdir ${PRETRAINED_CHECKPOINT_DIR} 39 | fi 40 | if [ ! -f ${PRETRAINED_CHECKPOINT_DIR}/inception_v1.ckpt ]; then 41 | wget http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz 42 | tar -xvf inception_v1_2016_08_28.tar.gz 43 | mv inception_v1.ckpt ${PRETRAINED_CHECKPOINT_DIR}/inception_v1.ckpt 44 | rm inception_v1_2016_08_28.tar.gz 45 | fi 46 | 47 | # Download the dataset 48 | python download_and_convert_data.py \ 49 | --dataset_name=flowers \ 50 | --dataset_dir=${DATASET_DIR} 51 | 52 | # Fine-tune only the new layers for 2000 steps. 53 | python train_image_classifier.py \ 54 | --train_dir=${TRAIN_DIR} \ 55 | --dataset_name=flowers \ 56 | --dataset_split_name=train \ 57 | --dataset_dir=${DATASET_DIR} \ 58 | --model_name=inception_v1 \ 59 | --checkpoint_path=${PRETRAINED_CHECKPOINT_DIR}/inception_v1.ckpt \ 60 | --checkpoint_exclude_scopes=InceptionV1/Logits \ 61 | --trainable_scopes=InceptionV1/Logits \ 62 | --max_number_of_steps=3000 \ 63 | --batch_size=32 \ 64 | --learning_rate=0.01 \ 65 | --save_interval_secs=60 \ 66 | --save_summaries_secs=60 \ 67 | --log_every_n_steps=100 \ 68 | --optimizer=rmsprop \ 69 | --weight_decay=0.00004 70 | 71 | # Run evaluation. 72 | python eval_image_classifier.py \ 73 | --checkpoint_path=${TRAIN_DIR} \ 74 | --eval_dir=${TRAIN_DIR} \ 75 | --dataset_name=flowers \ 76 | --dataset_split_name=validation \ 77 | --dataset_dir=${DATASET_DIR} \ 78 | --model_name=inception_v1 79 | 80 | # Fine-tune all the new layers for 1000 steps. 81 | python train_image_classifier.py \ 82 | --train_dir=${TRAIN_DIR}/all \ 83 | --dataset_name=flowers \ 84 | --dataset_split_name=train \ 85 | --dataset_dir=${DATASET_DIR} \ 86 | --checkpoint_path=${TRAIN_DIR} \ 87 | --model_name=inception_v1 \ 88 | --max_number_of_steps=1000 \ 89 | --batch_size=32 \ 90 | --learning_rate=0.001 \ 91 | --save_interval_secs=60 \ 92 | --save_summaries_secs=60 \ 93 | --log_every_n_steps=100 \ 94 | --optimizer=rmsprop \ 95 | --weight_decay=0.00004 96 | 97 | # Run evaluation. 98 | python eval_image_classifier.py \ 99 | --checkpoint_path=${TRAIN_DIR}/all \ 100 | --eval_dir=${TRAIN_DIR}/all \ 101 | --dataset_name=flowers \ 102 | --dataset_split_name=validation \ 103 | --dataset_dir=${DATASET_DIR} \ 104 | --model_name=inception_v1 105 | -------------------------------------------------------------------------------- /research/slim/scripts/finetune_inception_v3_on_flowers.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # This script performs the following operations: 18 | # 1. Downloads the Flowers dataset 19 | # 2. Fine-tunes an InceptionV3 model on the Flowers training set. 20 | # 3. Evaluates the model on the Flowers validation set. 21 | # 22 | # Usage: 23 | # cd slim 24 | # ./slim/scripts/finetune_inception_v3_on_flowers.sh 25 | set -e 26 | 27 | # Where the pre-trained InceptionV3 checkpoint is saved to. 28 | PRETRAINED_CHECKPOINT_DIR=/tmp/checkpoints 29 | 30 | # Where the training (fine-tuned) checkpoint and logs will be saved to. 31 | TRAIN_DIR=/tmp/flowers-models/inception_v3 32 | 33 | # Where the dataset is saved to. 34 | DATASET_DIR=/tmp/flowers 35 | 36 | # Download the pre-trained checkpoint. 37 | if [ ! -d "$PRETRAINED_CHECKPOINT_DIR" ]; then 38 | mkdir ${PRETRAINED_CHECKPOINT_DIR} 39 | fi 40 | if [ ! -f ${PRETRAINED_CHECKPOINT_DIR}/inception_v3.ckpt ]; then 41 | wget http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz 42 | tar -xvf inception_v3_2016_08_28.tar.gz 43 | mv inception_v3.ckpt ${PRETRAINED_CHECKPOINT_DIR}/inception_v3.ckpt 44 | rm inception_v3_2016_08_28.tar.gz 45 | fi 46 | 47 | # Download the dataset 48 | python download_and_convert_data.py \ 49 | --dataset_name=flowers \ 50 | --dataset_dir=${DATASET_DIR} 51 | 52 | # Fine-tune only the new layers for 1000 steps. 53 | python train_image_classifier.py \ 54 | --train_dir=${TRAIN_DIR} \ 55 | --dataset_name=flowers \ 56 | --dataset_split_name=train \ 57 | --dataset_dir=${DATASET_DIR} \ 58 | --model_name=inception_v3 \ 59 | --checkpoint_path=${PRETRAINED_CHECKPOINT_DIR}/inception_v3.ckpt \ 60 | --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ 61 | --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ 62 | --max_number_of_steps=1000 \ 63 | --batch_size=32 \ 64 | --learning_rate=0.01 \ 65 | --learning_rate_decay_type=fixed \ 66 | --save_interval_secs=60 \ 67 | --save_summaries_secs=60 \ 68 | --log_every_n_steps=100 \ 69 | --optimizer=rmsprop \ 70 | --weight_decay=0.00004 71 | 72 | # Run evaluation. 73 | python eval_image_classifier.py \ 74 | --checkpoint_path=${TRAIN_DIR} \ 75 | --eval_dir=${TRAIN_DIR} \ 76 | --dataset_name=flowers \ 77 | --dataset_split_name=validation \ 78 | --dataset_dir=${DATASET_DIR} \ 79 | --model_name=inception_v3 80 | 81 | # Fine-tune all the new layers for 500 steps. 82 | python train_image_classifier.py \ 83 | --train_dir=${TRAIN_DIR}/all \ 84 | --dataset_name=flowers \ 85 | --dataset_split_name=train \ 86 | --dataset_dir=${DATASET_DIR} \ 87 | --model_name=inception_v3 \ 88 | --checkpoint_path=${TRAIN_DIR} \ 89 | --max_number_of_steps=500 \ 90 | --batch_size=32 \ 91 | --learning_rate=0.0001 \ 92 | --learning_rate_decay_type=fixed \ 93 | --save_interval_secs=60 \ 94 | --save_summaries_secs=60 \ 95 | --log_every_n_steps=10 \ 96 | --optimizer=rmsprop \ 97 | --weight_decay=0.00004 98 | 99 | # Run evaluation. 100 | python eval_image_classifier.py \ 101 | --checkpoint_path=${TRAIN_DIR}/all \ 102 | --eval_dir=${TRAIN_DIR}/all \ 103 | --dataset_name=flowers \ 104 | --dataset_split_name=validation \ 105 | --dataset_dir=${DATASET_DIR} \ 106 | --model_name=inception_v3 107 | -------------------------------------------------------------------------------- /research/slim/scripts/finetune_resnet_v1_50_on_flowers.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # This script performs the following operations: 18 | # 1. Downloads the Flowers dataset 19 | # 2. Fine-tunes a ResNetV1-50 model on the Flowers training set. 20 | # 3. Evaluates the model on the Flowers validation set. 21 | # 22 | # Usage: 23 | # cd slim 24 | # ./slim/scripts/finetune_resnet_v1_50_on_flowers.sh 25 | set -e 26 | 27 | # Where the pre-trained ResNetV1-50 checkpoint is saved to. 28 | PRETRAINED_CHECKPOINT_DIR=/tmp/checkpoints 29 | 30 | # Where the training (fine-tuned) checkpoint and logs will be saved to. 31 | TRAIN_DIR=/tmp/flowers-models/resnet_v1_50 32 | 33 | # Where the dataset is saved to. 34 | DATASET_DIR=/tmp/flowers 35 | 36 | # Download the pre-trained checkpoint. 37 | if [ ! -d "$PRETRAINED_CHECKPOINT_DIR" ]; then 38 | mkdir ${PRETRAINED_CHECKPOINT_DIR} 39 | fi 40 | if [ ! -f ${PRETRAINED_CHECKPOINT_DIR}/resnet_v1_50.ckpt ]; then 41 | wget http://download.tensorflow.org/models/resnet_v1_50_2016_08_28.tar.gz 42 | tar -xvf resnet_v1_50_2016_08_28.tar.gz 43 | mv resnet_v1_50.ckpt ${PRETRAINED_CHECKPOINT_DIR}/resnet_v1_50.ckpt 44 | rm resnet_v1_50_2016_08_28.tar.gz 45 | fi 46 | 47 | # Download the dataset 48 | python download_and_convert_data.py \ 49 | --dataset_name=flowers \ 50 | --dataset_dir=${DATASET_DIR} 51 | 52 | # Fine-tune only the new layers for 3000 steps. 53 | python train_image_classifier.py \ 54 | --train_dir=${TRAIN_DIR} \ 55 | --dataset_name=flowers \ 56 | --dataset_split_name=train \ 57 | --dataset_dir=${DATASET_DIR} \ 58 | --model_name=resnet_v1_50 \ 59 | --checkpoint_path=${PRETRAINED_CHECKPOINT_DIR}/resnet_v1_50.ckpt \ 60 | --checkpoint_exclude_scopes=resnet_v1_50/logits \ 61 | --trainable_scopes=resnet_v1_50/logits \ 62 | --max_number_of_steps=3000 \ 63 | --batch_size=32 \ 64 | --learning_rate=0.01 \ 65 | --save_interval_secs=60 \ 66 | --save_summaries_secs=60 \ 67 | --log_every_n_steps=100 \ 68 | --optimizer=rmsprop \ 69 | --weight_decay=0.00004 70 | 71 | # Run evaluation. 72 | python eval_image_classifier.py \ 73 | --checkpoint_path=${TRAIN_DIR} \ 74 | --eval_dir=${TRAIN_DIR} \ 75 | --dataset_name=flowers \ 76 | --dataset_split_name=validation \ 77 | --dataset_dir=${DATASET_DIR} \ 78 | --model_name=resnet_v1_50 79 | 80 | # Fine-tune all the new layers for 1000 steps. 81 | python train_image_classifier.py \ 82 | --train_dir=${TRAIN_DIR}/all \ 83 | --dataset_name=flowers \ 84 | --dataset_split_name=train \ 85 | --dataset_dir=${DATASET_DIR} \ 86 | --checkpoint_path=${TRAIN_DIR} \ 87 | --model_name=resnet_v1_50 \ 88 | --max_number_of_steps=1000 \ 89 | --batch_size=32 \ 90 | --learning_rate=0.001 \ 91 | --save_interval_secs=60 \ 92 | --save_summaries_secs=60 \ 93 | --log_every_n_steps=100 \ 94 | --optimizer=rmsprop \ 95 | --weight_decay=0.00004 96 | 97 | # Run evaluation. 98 | python eval_image_classifier.py \ 99 | --checkpoint_path=${TRAIN_DIR}/all \ 100 | --eval_dir=${TRAIN_DIR}/all \ 101 | --dataset_name=flowers \ 102 | --dataset_split_name=validation \ 103 | --dataset_dir=${DATASET_DIR} \ 104 | --model_name=resnet_v1_50 105 | -------------------------------------------------------------------------------- /research/slim/scripts/train_cifarnet_on_cifar10.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # This script performs the following operations: 18 | # 1. Downloads the Cifar10 dataset 19 | # 2. Trains a CifarNet model on the Cifar10 training set. 20 | # 3. Evaluates the model on the Cifar10 testing set. 21 | # 22 | # Usage: 23 | # cd slim 24 | # ./scripts/train_cifarnet_on_cifar10.sh 25 | set -e 26 | 27 | # Where the checkpoint and logs will be saved to. 28 | TRAIN_DIR=/tmp/cifarnet-model 29 | 30 | # Where the dataset is saved to. 31 | DATASET_DIR=/tmp/cifar10 32 | 33 | # Download the dataset 34 | python download_and_convert_data.py \ 35 | --dataset_name=cifar10 \ 36 | --dataset_dir=${DATASET_DIR} 37 | 38 | # Run training. 39 | python train_image_classifier.py \ 40 | --train_dir=${TRAIN_DIR} \ 41 | --dataset_name=cifar10 \ 42 | --dataset_split_name=train \ 43 | --dataset_dir=${DATASET_DIR} \ 44 | --model_name=cifarnet \ 45 | --preprocessing_name=cifarnet \ 46 | --max_number_of_steps=100000 \ 47 | --batch_size=128 \ 48 | --save_interval_secs=120 \ 49 | --save_summaries_secs=120 \ 50 | --log_every_n_steps=100 \ 51 | --optimizer=sgd \ 52 | --learning_rate=0.1 \ 53 | --learning_rate_decay_factor=0.1 \ 54 | --num_epochs_per_decay=200 \ 55 | --weight_decay=0.004 56 | 57 | # Run evaluation. 58 | python eval_image_classifier.py \ 59 | --checkpoint_path=${TRAIN_DIR} \ 60 | --eval_dir=${TRAIN_DIR} \ 61 | --dataset_name=cifar10 \ 62 | --dataset_split_name=test \ 63 | --dataset_dir=${DATASET_DIR} \ 64 | --model_name=cifarnet 65 | -------------------------------------------------------------------------------- /research/slim/scripts/train_lenet_on_mnist.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # This script performs the following operations: 18 | # 1. Downloads the MNIST dataset 19 | # 2. Trains a LeNet model on the MNIST training set. 20 | # 3. Evaluates the model on the MNIST testing set. 21 | # 22 | # Usage: 23 | # cd slim 24 | # ./slim/scripts/train_lenet_on_mnist.sh 25 | set -e 26 | 27 | # Where the checkpoint and logs will be saved to. 28 | TRAIN_DIR=/tmp/lenet-model 29 | 30 | # Where the dataset is saved to. 31 | DATASET_DIR=/tmp/mnist 32 | 33 | # Download the dataset 34 | python download_and_convert_data.py \ 35 | --dataset_name=mnist \ 36 | --dataset_dir=${DATASET_DIR} 37 | 38 | # Run training. 39 | python train_image_classifier.py \ 40 | --train_dir=${TRAIN_DIR} \ 41 | --dataset_name=mnist \ 42 | --dataset_split_name=train \ 43 | --dataset_dir=${DATASET_DIR} \ 44 | --model_name=lenet \ 45 | --preprocessing_name=lenet \ 46 | --max_number_of_steps=20000 \ 47 | --batch_size=50 \ 48 | --learning_rate=0.01 \ 49 | --save_interval_secs=60 \ 50 | --save_summaries_secs=60 \ 51 | --log_every_n_steps=100 \ 52 | --optimizer=sgd \ 53 | --learning_rate_decay_type=fixed \ 54 | --weight_decay=0 55 | 56 | # Run evaluation. 57 | python eval_image_classifier.py \ 58 | --checkpoint_path=${TRAIN_DIR} \ 59 | --eval_dir=${TRAIN_DIR} \ 60 | --dataset_name=mnist \ 61 | --dataset_split_name=test \ 62 | --dataset_dir=${DATASET_DIR} \ 63 | --model_name=lenet 64 | -------------------------------------------------------------------------------- /research/slim/setup.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Setup script for slim.""" 16 | 17 | from setuptools import find_packages 18 | from setuptools import setup 19 | 20 | 21 | setup( 22 | name='slim', 23 | version='0.1', 24 | include_package_data=True, 25 | packages=find_packages(), 26 | description='tf-slim', 27 | ) 28 | --------------------------------------------------------------------------------