├── .gitignore ├── LICENSE ├── README.md ├── pics └── sparse.png └── sparse.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 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 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 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Torben Peters 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 | # sparse_convolution 2 | ![alt text](pics/sparse.png) 3 | 4 | Implementation of sparse convolution layer ofUhrig, Jonas, et al. 5 | 6 | "Sparsity Invariant CNNs." arXiv preprint arXiv:1708.06500 (2017). 7 | 8 | [Sparsity Invariant CNNs.](https://arxiv.org/abs/1708.06500) 9 | -------------------------------------------------------------------------------- /pics/sparse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PeterTor/sparse_convolution/244043a0a33e74189c859156fe6c2e297fda0753/pics/sparse.png -------------------------------------------------------------------------------- /sparse.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | """Arguments 4 | tensor: Tensor input. 5 | binary_mask: Tensor, a mask with the same size as tensor, channel size = 1 6 | filters: Integer, the dimensionality of the output space (i.e. the number 7 | of filters in the convolution). 8 | kernel_size: An integer or tuple/list of 2 integers, specifying the 9 | height and width of the 2D convolution window. 10 | strides: An integer or tuple/list of 2 integers, 11 | specifying the strides of the convolution along the height and width. 12 | l2_scale: float, A scalar multiplier Tensor. 0.0 disables the regularizer. 13 | 14 | 15 | Returns: 16 | Output tensor, binary mask. 17 | """ 18 | 19 | def sparse_conv(tensor,binary_mask = None,filters=32,kernel_size=3,strides=2,l2_scale=0.0): 20 | 21 | if binary_mask == None: #first layer has no binary mask 22 | b,h,w,c = tensor.get_shape() 23 | channels=tf.split(tensor,c,axis=3) 24 | #assume that if one channel has no information, all channels have no information 25 | binary_mask = tf.where(tf.equal(channels[0], 0), tf.zeros_like(channels[0]), tf.ones_like(channels[0])) #mask should only have the size of (B,H,W,1) 26 | 27 | features = tf.multiply(tensor,binary_mask) 28 | features = tf.layers.conv2d(features, filters=filters, kernel_size=kernel_size, strides=(strides, strides), trainable=True, use_bias=False, padding="same",kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=l2_scale)) 29 | 30 | norm = tf.layers.conv2d(binary_mask, filters=filters,kernel_size=kernel_size,strides=(strides, strides),kernel_initializer=tf.ones_initializer(),trainable=False,use_bias=False,padding="same") 31 | norm = tf.where(tf.equal(norm,0),tf.zeros_like(norm),tf.reciprocal(norm)) 32 | _,_,_,bias_size = norm.get_shape() 33 | 34 | b = tf.Variable(tf.constant(0.0, shape=[bias_size]),trainable=True) 35 | feature = tf.multiply(features,norm)+b 36 | mask = tf.layers.max_pooling2d(binary_mask,strides = strides,pool_size=kernel_size,padding="same") 37 | 38 | return feature,mask 39 | 40 | 41 | 42 | image = tf.placeholder(tf.float32, shape=[None,64,64,2], name="input_image") 43 | b_mask = tf.placeholder(tf.float32, shape=[None,64,64,1], name="binary_mask") 44 | features,b_mask = sparse_conv(image) 45 | features,b_mask = sparse_conv(features,binary_mask=b_mask) 46 | 47 | sess = tf.Session() 48 | sess.run(tf.global_variables_initializer()) 49 | 50 | --------------------------------------------------------------------------------