├── .DS_Store ├── .gitignore ├── LICENSE ├── README.md ├── assets ├── .DS_Store ├── activation_map.png ├── best_top1_acc.png ├── classification.png ├── imagenet_classification.png ├── partial_conv.png ├── segmentation_1.png └── segmentation_2.png └── partial_conv.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/.DS_Store -------------------------------------------------------------------------------- /.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 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Junho Kim (1993.01.12) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PartialConv-Tensorflow 2 | Simple Tensorflow implementation of [Partial Convolution based Padding](https://arxiv.org/abs/1811.11718) 3 | ![partial_conv](./assets/partial_conv.png) 4 | 5 | ## How to use 6 | ```python 7 | # typical convolution layer with zero padding 8 | x = conv(x, channels, kernel=3, stride=2, use_bias=True, padding='SAME', scope='conv') 9 | 10 | # partial convolution based padding 11 | x = partial_conv(x, channels, kernel=3, stride=2, use_bias=True, padding='SAME', scope='conv') 12 | 13 | ``` 14 | ## Results 15 | ### Activation map 16 | ![activation_map](./assets/activation_map.png) 17 | 18 | ### ImageNet Classification 19 | ![classification](./assets/classification.png) 20 | 21 | 22 | ### Segmentation (DeepLab V3+) 23 | ![seg_1](./assets/segmentation_1.png) 24 | ![seg_2](./assets/segmentation_2.png) 25 | 26 | 27 | ## Author 28 | Junho Kim 29 | -------------------------------------------------------------------------------- /assets/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/assets/.DS_Store -------------------------------------------------------------------------------- /assets/activation_map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/assets/activation_map.png -------------------------------------------------------------------------------- /assets/best_top1_acc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/assets/best_top1_acc.png -------------------------------------------------------------------------------- /assets/classification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/assets/classification.png -------------------------------------------------------------------------------- /assets/imagenet_classification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/assets/imagenet_classification.png -------------------------------------------------------------------------------- /assets/partial_conv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/assets/partial_conv.png -------------------------------------------------------------------------------- /assets/segmentation_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/assets/segmentation_1.png -------------------------------------------------------------------------------- /assets/segmentation_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/taki0112/partial_conv-Tensorflow/9fecc66d847b17a36ac89e09abe78a4d28b7df53/assets/segmentation_2.png -------------------------------------------------------------------------------- /partial_conv.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow.contrib as tf_contrib 3 | 4 | # Xavier : tf_contrib.layers.xavier_initializer() 5 | # He : tf_contrib.layers.variance_scaling_initializer() 6 | # Normal : tf.random_normal_initializer(mean=0.0, stddev=0.02) 7 | # l2_decay : tf_contrib.layers.l2_regularizer(0.0001) 8 | 9 | weight_init = tf.random_normal_initializer(mean=0.0, stddev=0.02) 10 | weight_regularizer = tf_contrib.layers.l2_regularizer(scale=0.0001) 11 | 12 | 13 | ################################################################################## 14 | # Layer 15 | ################################################################################## 16 | def partial_conv(x, channels, kernel=3, stride=2, use_bias=True, padding='SAME', scope='conv_0'): 17 | with tf.variable_scope(scope): 18 | if padding.lower() == 'SAME'.lower() : 19 | with tf.variable_scope('mask'): 20 | _, h, w, _ = x.get_shape().as_list() 21 | 22 | slide_window = kernel * kernel 23 | mask = tf.ones(shape=[1, h, w, 1]) 24 | 25 | update_mask = tf.layers.conv2d(mask, filters=1, 26 | kernel_size=kernel, kernel_initializer=tf.constant_initializer(1.0), 27 | strides=stride, padding=padding, use_bias=False, trainable=False) 28 | 29 | mask_ratio = slide_window / (update_mask + 1e-8) 30 | update_mask = tf.clip_by_value(update_mask, 0.0, 1.0) 31 | mask_ratio = mask_ratio * update_mask 32 | 33 | with tf.variable_scope('x'): 34 | x = tf.layers.conv2d(x, filters=channels, 35 | kernel_size=kernel, kernel_initializer=weight_init, 36 | kernel_regularizer=weight_regularizer, 37 | strides=stride, padding=padding, use_bias=False) 38 | x = x * mask_ratio 39 | 40 | if use_bias: 41 | bias = tf.get_variable("bias", [channels], initializer=tf.constant_initializer(0.0)) 42 | 43 | x = tf.nn.bias_add(x, bias) 44 | x = x * update_mask 45 | 46 | else : 47 | x = tf.layers.conv2d(x, filters=channels, 48 | kernel_size=kernel, kernel_initializer=weight_init, 49 | kernel_regularizer=weight_regularizer, 50 | strides=stride, padding=padding, use_bias=use_bias) 51 | 52 | return x 53 | --------------------------------------------------------------------------------