def _GetVal(use_xla): with self.session(): t0 = array_ops.placeholder(np.float32, shape=input_sizes) t1 = constant_op.constant(filter_sizes, shape=[len(filter_sizes)]) t2 = array_ops.placeholder(np.float32, shape=output_sizes) native_t0 = t0 native_t2 = t2 strides = [1, stride, stride, 1] dilations = [1, dilation, dilation, 1] if use_xla: if data_format == "NCHW": # Transpose from NWHC input to NCHW # Ex. [4, 5, 5, 48] to [4, 48, 5, 5] native_t0 = array_ops.transpose(t0, [0, 3, 1, 2]) native_t2 = array_ops.transpose(t2, [0, 3, 1, 2]) strides = [1, 1, stride, stride] dilations = [1, 1, dilation, dilation] with self.test_scope(): backprop = nn_ops.depthwise_conv2d_native_backprop_filter( native_t0, t1, native_t2, strides=strides, padding=padding, dilations=dilations, data_format=data_format) else: # For CPU, the format NCHW is not supported. Therefore we always use # NHWC here. # depthwise_conv2d_native_backprop_filter on CPU doesn't support # dilation. native_t3 = array_ops.space_to_batch(native_t2, block_size=dilation, paddings=[[0, 0], [0, 0]]) native_t0_transform = array_ops.space_to_batch( native_t0, block_size=dilation, paddings=[[0, 0], [0, 0]]) backprop = nn_ops.depthwise_conv2d_native_backprop_filter( native_t0_transform, t1, native_t3, strides=strides, padding=padding) ret = backprop.eval({t0: x0, t2: x2}) self.assertShapeEqual(ret, backprop) return ret
def _BatchToSpaceGrad(op, grad): # Its gradient is the opposite op: SpaceToBatch. block_size = op.get_attr("block_size") return [ array_ops.space_to_batch(grad, op.inputs[1], block_size=block_size), None ]
def _GetVal(use_xla): with self.session(): t1 = array_ops.placeholder(np.float32, shape=filter_sizes) t2 = array_ops.placeholder(np.float32, shape=output_sizes) if use_xla: with self.test_scope(): t0 = constant_op.constant(input_sizes, shape=[len(input_sizes)]) backprop = nn_ops.depthwise_conv2d_native_backprop_input( t0, t1, t2, strides=[1, stride, stride, 1], dilations=[1, dilation, dilation, 1], padding=padding) else: # TODO(wangtao): figure out gradient with stride > 1. # depthwise_conv2d_native_backprop_input on CPU doesn't support # dilation. t3 = array_ops.space_to_batch( t2, block_size=dilation, paddings=[[0, 0], [0, 0]]) input_sizes_transform = [ input_sizes[0] * dilation * dilation, input_sizes[1] // dilation, input_sizes[2] // dilation, input_sizes[3] ] t0 = constant_op.constant( input_sizes_transform, shape=[len(input_sizes)]) backprop_naive = nn_ops.depthwise_conv2d_native_backprop_input( t0, t1, t3, strides=[1, stride, stride, 1], padding=padding) backprop = array_ops.batch_to_space( backprop_naive, [[0, 0], [0, 0]], block_size=dilation) ret = backprop.eval({t1: x1, t2: x2}) self.assertShapeEqual(ret, backprop) return ret
def testAtrousSequence(self): """Tests optimization of sequence of atrous convolutions. Verifies that a sequence of `atrous_conv2d` operations with identical `rate` parameters, 'SAME' `padding`, and `filters` with odd heights/ widths: net = atrous_conv2d(net, filters1, rate, padding="SAME") net = atrous_conv2d(net, filters2, rate, padding="SAME") ... net = atrous_conv2d(net, filtersK, rate, padding="SAME") is equivalent to: pad = ... # padding so that the input dims are multiples of rate net = space_to_batch(net, paddings=pad, block_size=rate) net = conv2d(net, filters1, strides=[1, 1, 1, 1], padding="SAME") net = conv2d(net, filters2, strides=[1, 1, 1, 1], padding="SAME") ... net = conv2d(net, filtersK, strides=[1, 1, 1, 1], padding="SAME") net = batch_to_space(net, crops=pad, block_size=rate) """ padding = "SAME" # The padding needs to be "SAME" np.random.seed(1) # Make it reproducible. with self.session(use_gpu=True): # Input: [batch, height, width, input_depth] for height in range(15, 17): for width in range(15, 17): x_shape = [3, height, width, 2] x = np.random.random_sample(x_shape).astype(np.float32) for kernel in [1, 3, 5]: # The kernel size needs to be odd. # Filter: [kernel_height, kernel_width, input_depth, output_depth] f_shape = [kernel, kernel, 2, 2] f = 1e-2 * np.random.random_sample(f_shape).astype(np.float32) for rate in range(2, 4): # y1: three atrous_conv2d in a row. y1 = nn_ops.atrous_conv2d(x, f, rate, padding=padding) y1 = nn_ops.atrous_conv2d(y1, f, rate, padding=padding) y1 = nn_ops.atrous_conv2d(y1, f, rate, padding=padding) # y2: space_to_batch, three conv2d in a row, batch_to_space pad_bottom = 0 if height % rate == 0 else rate - height % rate pad_right = 0 if width % rate == 0 else rate - width % rate pad = [[0, pad_bottom], [0, pad_right]] y2 = array_ops.space_to_batch(x, paddings=pad, block_size=rate) y2 = nn_ops.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding) y2 = nn_ops.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding) y2 = nn_ops.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding) y2 = array_ops.batch_to_space(y2, crops=pad, block_size=rate) self.assertAllClose( y1.eval(), self.evaluate(y2), rtol=1e-2, atol=1e-2)
def _BatchToSpaceGrad(op, grad): # Its gradient is the opposite op: SpaceToBatch. block_size = op.get_attr("block_size") return [array_ops.space_to_batch(grad, op.inputs[1], block_size=block_size), None]
def space_to_batch(*args, **kwargs): return array_ops.space_to_batch(*args, **kwargs)
def atrous_conv2d(value, filters, rate, padding, name=None): """Atrous convolution (a.k.a. convolution with holes or dilated convolution). Computes a 2-D atrous convolution, also known as convolution with holes or dilated convolution, given 4-D `value` and `filters` tensors. If the `rate` parameter is equal to one, it performs regular 2-D convolution. If the `rate` parameter is greater than one, it performs convolution with holes, sampling the input values every `rate` pixels in the `height` and `width` dimensions. This is equivalent to convolving the input with a set of upsampled filters, produced by inserting `rate - 1` zeros between two consecutive values of the filters along the `height` and `width` dimensions, hence the name atrous convolution or convolution with holes (the French word trous means holes in English). More specifically: output[b, i, j, k] = sum_{di, dj, q} filters[di, dj, q, k] * value[b, i + rate * di, j + rate * dj, q] Atrous convolution allows us to explicitly control how densely to compute feature responses in fully convolutional networks. Used in conjunction with bilinear interpolation, it offers an alternative to `conv2d_transpose` in dense prediction tasks such as semantic image segmentation, optical flow computation, or depth estimation. It also allows us to effectively enlarge the field of view of filters without increasing the number of parameters or the amount of computation. For a description of atrous convolution and how it can be used for dense feature extraction, please see: [Semantic Image Segmentation with Deep Convolutional Nets and Fully Connected CRFs](http://arxiv.org/abs/1412.7062). The same operation is investigated further in [Multi-Scale Context Aggregation by Dilated Convolutions](http://arxiv.org/abs/1511.07122). Previous works that effectively use atrous convolution in different ways are, among others, [OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks](http://arxiv.org/abs/1312.6229) and [Fast Image Scanning with Deep Max-Pooling Convolutional Neural Networks] (http://arxiv.org/abs/1302.1700). Atrous convolution is also closely related to the so-called noble identities in multi-rate signal processing. There are many different ways to implement atrous convolution (see the refs above). The implementation here reduces atrous_conv2d(value, filters, rate, padding=padding) to the following three operations: paddings = ... net = space_to_batch(value, paddings, block_size=rate) net = conv2d(net, filters, strides=[1, 1, 1, 1], padding="VALID") crops = ... net = batch_to_space(net, crops, block_size=rate) Advanced usage. Note the following optimization: A sequence of `atrous_conv2d` operations with identical `rate` parameters, 'SAME' `padding`, and filters with odd heights/ widths: net = atrous_conv2d(net, filters1, rate, padding="SAME") net = atrous_conv2d(net, filters2, rate, padding="SAME") ... net = atrous_conv2d(net, filtersK, rate, padding="SAME") can be equivalently performed cheaper in terms of computation and memory as: pad = ... # padding so that the input dims are multiples of rate net = space_to_batch(net, paddings=pad, block_size=rate) net = conv2d(net, filters1, strides=[1, 1, 1, 1], padding="SAME") net = conv2d(net, filters2, strides=[1, 1, 1, 1], padding="SAME") ... net = conv2d(net, filtersK, strides=[1, 1, 1, 1], padding="SAME") net = batch_to_space(net, crops=pad, block_size=rate) because a pair of consecutive `space_to_batch` and `batch_to_space` ops with the same `block_size` cancel out when their respective `paddings` and `crops` inputs are identical. Args: value: A 4-D `Tensor` of type `float`. It needs to be in the default "NHWC" format. Its shape is `[batch, in_height, in_width, in_channels]`. filters: A 4-D `Tensor` with the same type as `value` and shape `[filter_height, filter_width, in_channels, out_channels]`. `filters`' `in_channels` dimension must match that of `value`. Atrous convolution is equivalent to standard convolution with upsampled filters with effective height `filter_height + (filter_height - 1) * (rate - 1)` and effective width `filter_width + (filter_width - 1) * (rate - 1)`, produced by inserting `rate - 1` zeros along consecutive elements across the `filters`' spatial dimensions. rate: A positive int32. The stride with which we sample input values across the `height` and `width` dimensions. Equivalently, the rate by which we upsample the filter values by inserting zeros across the `height` and `width` dimensions. In the literature, the same parameter is sometimes called `input stride` or `dilation`. padding: A string, either `'VALID'` or `'SAME'`. The padding algorithm. name: Optional name for the returned tensor. Returns: A `Tensor` with the same type as `value`. Raises: ValueError: If input/output depth does not match `filters`' shape, or if padding is other than `'VALID'` or `'SAME'`. """ with ops.op_scope([value, filters], name, "atrous_conv2d") as name: value = ops.convert_to_tensor(value, name="value") filters = ops.convert_to_tensor(filters, name="filters") value_shape = value.get_shape() filter_shape = filters.get_shape() if not value_shape[3].is_compatible_with(filter_shape[2]): raise ValueError( "value's input channels does not match filters' input channels, " "{} != {}".format(value_shape[3], filter_shape[2])) if rate < 1: raise ValueError("rate {} cannot be less than one".format(rate)) if rate == 1: value = gen_nn_ops.conv2d(input=value, filter=filters, strides=[1, 1, 1, 1], padding=padding) return value # We have two padding contributions. The first is used for converting "SAME" # to "VALID". The second is required so that the height and width of the # zero-padded value tensor are multiples of rate. # Spatial dimensions of original input value_shape = array_ops.shape(value) in_height = value_shape[1] in_width = value_shape[2] # Spatial dimensions of the filters and the upsampled filters in which we # introduce (rate - 1) zeros between consecutive filter values. filter_height = int(filter_shape[0]) filter_width = int(filter_shape[1]) filter_height_up = filter_height + (filter_height - 1) * (rate - 1) filter_width_up = filter_width + (filter_width - 1) * (rate - 1) # Padding required to reduce to "VALID" convolution if padding == "SAME": pad_height = filter_height_up - 1 pad_width = filter_width_up - 1 elif padding == "VALID": pad_height = 0 pad_width = 0 else: raise ValueError("Invalid padding") # When padding is "SAME" and the pad_height (pad_width) is odd, we pad more # to bottom (right), following the same convention as conv2d(). pad_top = math_ops.floordiv(pad_height, 2) pad_bottom = pad_height - pad_top pad_left = math_ops.floordiv(pad_width, 2) pad_right = pad_width - pad_left # More padding so that rate divides the height and width of the input value in_height = in_height + pad_top + pad_bottom in_width = in_width + pad_left + pad_right mod_height = math_ops.mod(in_height, rate) mod_width = math_ops.mod(in_width, rate) null = constant_op.constant(0) pad_bottom_extra = control_flow_ops.cond(gen_math_ops.equal(mod_height, 0), lambda: null, lambda: rate - mod_height) pad_right_extra = control_flow_ops.cond(gen_math_ops.equal(mod_width, 0), lambda: null, lambda: rate - mod_width) # The paddings argument to space_to_batch includes both padding components pad_bottom = pad_bottom + pad_bottom_extra pad_right = pad_right + pad_right_extra space_to_batch_pad = [[pad_top, pad_bottom], [pad_left, pad_right]] value = array_ops.space_to_batch(input=value, paddings=space_to_batch_pad, block_size=rate) value = gen_nn_ops.conv2d(input=value, filter=filters, strides=[1, 1, 1, 1], padding="VALID", name=name) # The crops argument to batch_to_space is just the extra padding component batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]] value = array_ops.batch_to_space(input=value, crops=batch_to_space_crop, block_size=rate) return value
def atrous_conv2d(value, filters, rate, padding, name=None): """Atrous convolution (a.k.a. convolution with holes or dilated convolution). Computes a 2-D atrous convolution, also known as convolution with holes or dilated convolution, given 4-D `value` and `filters` tensors. If the `rate` parameter is equal to one, it performs regular 2-D convolution. If the `rate` parameter is greater than one, it performs convolution with holes, sampling the input values every `rate` pixels in the `height` and `width` dimensions. This is equivalent to convolving the input with a set of upsampled filters, produced by inserting `rate - 1` zeros between two consecutive values of the filters along the `height` and `width` dimensions, hence the name atrous convolution or convolution with holes (the French word trous means holes in English). More specifically: output[b, i, j, k] = sum_{di, dj, q} filters[di, dj, q, k] * value[b, i + rate * di, j + rate * dj, q] Atrous convolution allows us to explicitly control how densely to compute feature responses in fully convolutional networks. Used in conjunction with bilinear interpolation, it offers an alternative to `conv2d_transpose` in dense prediction tasks such as semantic image segmentation, optical flow computation, or depth estimation. It also allows us to effectively enlarge the field of view of filters without increasing the number of parameters or the amount of computation. For a description of atrous convolution and how it can be used for dense feature extraction, please see: [Semantic Image Segmentation with Deep Convolutional Nets and Fully Connected CRFs](http://arxiv.org/abs/1412.7062). The same operation is investigated further in [Multi-Scale Context Aggregation by Dilated Convolutions](http://arxiv.org/abs/1511.07122). Previous works that effectively use atrous convolution in different ways are, among others, [OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks](http://arxiv.org/abs/1312.6229) and [Fast Image Scanning with Deep Max-Pooling Convolutional Neural Networks] (http://arxiv.org/abs/1302.1700). Atrous convolution is also closely related to the so-called noble identities in multi-rate signal processing. There are many different ways to implement atrous convolution (see the refs above). The implementation here reduces atrous_conv2d(value, filters, rate, padding=padding) to the following three operations: paddings = ... net = space_to_batch(value, paddings, block_size=rate) net = conv2d(net, filters, strides=[1, 1, 1, 1], padding="VALID") crops = ... net = batch_to_space(net, crops, block_size=rate) Advanced usage. Note the following optimization: A sequence of `atrous_conv2d` operations with identical `rate` parameters, 'SAME' `padding`, and filters with odd heights/ widths: net = atrous_conv2d(net, filters1, rate, padding="SAME") net = atrous_conv2d(net, filters2, rate, padding="SAME") ... net = atrous_conv2d(net, filtersK, rate, padding="SAME") can be equivalently performed cheaper in terms of computation and memory as: pad = ... # padding so that the input dims are multiples of rate net = space_to_batch(net, paddings=pad, block_size=rate) net = conv2d(net, filters1, strides=[1, 1, 1, 1], padding="SAME") net = conv2d(net, filters2, strides=[1, 1, 1, 1], padding="SAME") ... net = conv2d(net, filtersK, strides=[1, 1, 1, 1], padding="SAME") net = batch_to_space(net, crops=pad, block_size=rate) because a pair of consecutive `space_to_batch` and `batch_to_space` ops with the same `block_size` cancel out when their respective `paddings` and `crops` inputs are identical. Args: value: A 4-D `Tensor` of type `float`. It needs to be in the default "NHWC" format. Its shape is `[batch, in_height, in_width, in_channels]`. filters: A 4-D `Tensor` with the same type as `value` and shape `[filter_height, filter_width, in_channels, out_channels]`. `filters`' `in_channels` dimension must match that of `value`. Atrous convolution is equivalent to standard convolution with upsampled filters with effective height `filter_height + (filter_height - 1) * (rate - 1)` and effective width `filter_width + (filter_width - 1) * (rate - 1)`, produced by inserting `rate - 1` zeros along consecutive elements across the `filters`' spatial dimensions. rate: A positive int32. The stride with which we sample input values across the `height` and `width` dimensions. Equivalently, the rate by which we upsample the filter values by inserting zeros across the `height` and `width` dimensions. In the literature, the same parameter is sometimes called `input stride` or `dilation`. padding: A string, either `'VALID'` or `'SAME'`. The padding algorithm. name: Optional name for the returned tensor. Returns: A `Tensor` with the same type as `value`. Raises: ValueError: If input/output depth does not match `filters`' shape, or if padding is other than `'VALID'` or `'SAME'`. """ with ops.op_scope([value, filters], name, "atrous_conv2d") as name: value = ops.convert_to_tensor(value, name="value") filters = ops.convert_to_tensor(filters, name="filters") value_shape = value.get_shape() filter_shape = filters.get_shape() if not value_shape[3].is_compatible_with(filter_shape[2]): raise ValueError( "value's input channels does not match filters' input channels, " "{} != {}".format(value_shape[3], filter_shape[2])) if rate < 1: raise ValueError("rate {} cannot be less than one".format(rate)) if rate == 1: value = gen_nn_ops.conv2d(input=value, filter=filters, strides=[1, 1, 1, 1], padding=padding) return value # We have two padding contributions. The first is used for converting "SAME" # to "VALID". The second is required so that the height and width of the # zero-padded value tensor are multiples of rate. # Spatial dimensions of original input in_height = int(value_shape[1]) in_width = int(value_shape[2]) # Spatial dimensions of the filters and the upsampled filters in which we # introduce (rate - 1) zeros between consecutive filter values. filter_height = int(filter_shape[0]) filter_width = int(filter_shape[1]) filter_height_up = filter_height + (filter_height - 1) * (rate - 1) filter_width_up = filter_width + (filter_width - 1) * (rate - 1) # Padding required to reduce to "VALID" convolution if padding == "SAME": pad_height = filter_height_up - 1 pad_width = filter_width_up - 1 elif padding == "VALID": pad_height = 0 pad_width = 0 else: raise ValueError("Invalid padding") # When padding is "SAME" and the pad_height (pad_width) is odd, we pad more # to bottom (right), following the same convention as conv2d(). pad_top = pad_height // 2 pad_bottom = pad_height - pad_top pad_left = pad_width // 2 pad_right = pad_width - pad_left # More padding so that rate divides the height and width of the input value in_height = in_height + pad_top + pad_bottom in_width = in_width + pad_left + pad_right pad_bottom_extra = 0 if in_height % rate == 0 else rate - in_height % rate pad_right_extra = 0 if in_width % rate == 0 else rate - in_width % rate # The paddings argument to space_to_batch includes both padding components space_to_batch_pad = [[pad_top, pad_bottom + pad_bottom_extra], [pad_left, pad_right + pad_right_extra]] value = array_ops.space_to_batch(input=value, paddings=space_to_batch_pad, block_size=rate) value = gen_nn_ops.conv2d(input=value, filter=filters, strides=[1, 1, 1, 1], padding="VALID", name=name) # The crops argument to batch_to_space is just the extra padding component batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]] value = array_ops.batch_to_space(input=value, crops=batch_to_space_crop, block_size=rate) return value
def atrous_avg_pool(value, size, rate, padding, name=None, info=DummyDict()): with tfops.op_scope([value], name, "atrous_avg_pool") as name: value = tfops.convert_to_tensor(value, name="value") if rate < 1: raise ValueError("rate {} cannot be less than one".format(rate)) if rate == 1: value = nn_ops.avg_pool(value=value, strides=[1, 1, 1, 1], ksize=[1, size, size, 1], padding=padding) return value # We have two padding contributions. The first is used for converting "SAME" # to "VALID". The second is required so that the height and width of the # zero-padded value tensor are multiples of rate. # Padding required to reduce to "VALID" convolution if padding == "SAME": filter_height, filter_width = size, size # Spatial dimensions of the filters and the upsampled filters in which we # introduce (rate - 1) zeros between consecutive filter values. filter_height_up = filter_height + (filter_height - 1) * (rate - 1) filter_width_up = filter_width + (filter_width - 1) * (rate - 1) pad_height = filter_height_up - 1 pad_width = filter_width_up - 1 # When pad_height (pad_width) is odd, we pad more to bottom (right), # following the same convention as avg_pool(). pad_top = pad_height // 2 pad_bottom = pad_height - pad_top pad_left = pad_width // 2 pad_right = pad_width - pad_left elif padding == "VALID": pad_top = 0 pad_bottom = 0 pad_left = 0 pad_right = 0 else: raise ValueError("Invalid padding") # Handle input whose shape is unknown during graph creation. if value.get_shape().is_fully_defined(): value_shape = value.get_shape().as_list() else: value_shape = array_ops.shape(value) in_height = value_shape[1] + pad_top + pad_bottom in_width = value_shape[2] + pad_left + pad_right # More padding so that rate divides the height and width of the input. pad_bottom_extra = (rate - in_height % rate) % rate pad_right_extra = (rate - in_width % rate) % rate # The paddings argument to space_to_batch includes both padding components. space_to_batch_pad = [[pad_top, pad_bottom + pad_bottom_extra], [pad_left, pad_right + pad_right_extra]] value = array_ops.space_to_batch(input=value, paddings=space_to_batch_pad, block_size=rate) value = nn_ops.avg_pool(value=value, ksize=[1, size, size, 1], strides=[1, 1, 1, 1], padding="VALID", name=name) # The crops argument to batch_to_space is just the extra padding component. batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]] value = array_ops.batch_to_space(input=value, crops=batch_to_space_crop, block_size=rate) info['activations'][name] = value return value
def atrous_pool2d(value,ksize, rate, padding, name=None, pooling_type="MAX"): with ops.name_scope(name, "atrous_pool2d", [value]) as name: value = ops.convert_to_tensor(value, name="value") if rate < 1: raise ValueError("rate {} cannot be less than one".format(rate)) if rate == 1: if pooling_type == "MAX": value = nn_ops.max_pool(value=value, ksize=ksize, strides=[1, 1, 1, 1], padding=padding) return value elif pooling_type == "AVG": value = nn_ops.avg_pool(value=value, ksize=ksize, strides=[1, 1, 1, 1], padding=padding) return value else: raise ValueError("Invalid pooling type") # We have two padding contributions. The first is used for converting "SAME" # to "VALID". The second is required so that the height and width of the # zero-padded value tensor are multiples of rate. # Padding required to reduce to "VALID" convolution if padding == "SAME": # Handle filters whose shape is unknown during graph creation. # if filters.get_shape().is_fully_defined(): # filter_shape = filters.get_shape().as_list() # else: # filter_shape = array_ops.shape(filters) # filter_height, filter_width = filter_shape[0], filter_shape[1] kernel_height, kernel_width = ksize[1], ksize[2] # Spatial dimensions of the filters and the upsampled filters in which we # introduce (rate - 1) zeros between consecutive filter values. kernel_height_up = kernel_height + (kernel_height - 1) * (rate - 1) kernel_width_up = kernel_width + (kernel_width - 1) * (rate - 1) pad_height = kernel_height_up - 1 pad_width = kernel_width_up - 1 # When pad_height (pad_width) is odd, we pad more to bottom (right), # following the same convention as conv2d(). pad_top = pad_height // 2 pad_bottom = pad_height - pad_top pad_left = pad_width // 2 pad_right = pad_width - pad_left elif padding == "VALID": pad_top = 0 pad_bottom = 0 pad_left = 0 pad_right = 0 else: raise ValueError("Invalid padding") # Handle input whose shape is unknown during graph creation. if value.get_shape().is_fully_defined(): value_shape = value.get_shape().as_list() else: value_shape = array_ops.shape(value) in_height = value_shape[1] + pad_top + pad_bottom in_width = value_shape[2] + pad_left + pad_right # More padding so that rate divides the height and width of the input. pad_bottom_extra = (rate - in_height % rate) % rate pad_right_extra = (rate - in_width % rate) % rate # The paddings argument to space_to_batch includes both padding components. space_to_batch_pad = [[pad_top, pad_bottom + pad_bottom_extra], [pad_left, pad_right + pad_right_extra]] value = array_ops.space_to_batch(input=value, paddings=space_to_batch_pad, block_size=rate) if pooling_type == "MAX": value = nn_ops.max_pool(value=value, ksize=ksize, strides=[1, 1, 1, 1], padding="VALID", name=name) elif pooling_type == "AVG": value = nn_ops.avg_pool(value=value, ksize=ksize, strides=[1, 1, 1, 1], padding="VALID", name=name) else: raise ValueError("Invalid pooling type") # The crops argument to batch_to_space is just the extra padding component. batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]] value = array_ops.batch_to_space(input=value, crops=batch_to_space_crop, block_size=rate) return value
def atrous_conv2d(value, filters, rate, padding, name=None): with ops.op_scope([value, filters], name, "atrous_conv2d") as name: value = ops.convert_to_tensor(value, name="value") filters = ops.convert_to_tensor(filters, name="filters") value_shape = value.get_shape() filter_shape = filters.get_shape() if not value_shape[3].is_compatible_with(filter_shape[2]): raise ValueError( "value's input channels does not match filters' input channels, " "{} != {}".format(value_shape[3], filter_shape[2])) if rate < 1: raise ValueError("rate {} cannot be less than one".format(rate)) if rate == 1: value = gen_nn_ops.conv2d(input=value, filter=filters, strides=[1, 1, 1, 1], padding=padding) return value # We have two padding contributions. The first is used for converting "SAME" # to "VALID". The second is required so that the height and width of the # zero-padded value tensor are multiples of rate. # Spatial dimensions of original input value_shape = array_ops.shape(value) in_height = value_shape[1] in_width = value_shape[2] # Spatial dimensions of the filters and the upsampled filters in which we # introduce (rate - 1) zeros between consecutive filter values. filter_shape = array_ops.shape(filters) filter_height = filter_shape[0] filter_width = filter_shape[1] filter_height_up = filter_height + (filter_height - 1) * (rate - 1) filter_width_up = filter_width + (filter_width - 1) * (rate - 1) # Padding required to reduce to "VALID" convolution if padding == "SAME": pad_height = filter_height_up - 1 pad_width = filter_width_up - 1 elif padding == "VALID": pad_height = 0 pad_width = 0 else: raise ValueError("Invalid padding") # When padding is "SAME" and the pad_height (pad_width) is odd, we pad more # to bottom (right), following the same convention as conv2d(). pad_top = math_ops.floordiv(pad_height, 2) pad_bottom = pad_height - pad_top pad_left = math_ops.floordiv(pad_width, 2) pad_right = pad_width - pad_left # More padding so that rate divides the height and width of the input value in_height = in_height + pad_top + pad_bottom in_width = in_width + pad_left + pad_right mod_height = math_ops.mod(in_height, rate) mod_width = math_ops.mod(in_width, rate) null = constant_op.constant(0) pad_bottom_extra = control_flow_ops.cond(gen_math_ops.equal(mod_height, 0), lambda: null, lambda: rate - mod_height) pad_right_extra = control_flow_ops.cond(gen_math_ops.equal(mod_width, 0), lambda: null, lambda: rate - mod_width) # The paddings argument to space_to_batch includes both padding components pad_bottom = pad_bottom + pad_bottom_extra pad_right = pad_right + pad_right_extra print 'hahahaha' v = array_ops.expand_dims(array_ops.pack([pad_top, pad_bottom]),1) h = array_ops.expand_dims(array_ops.pack([pad_left, pad_right]),1) space_to_batch_pad = array_ops.concat(1, [v,h]) space_to_batch_pad = [[pad_top, pad_bottom], [pad_left, pad_right]] value = array_ops.space_to_batch(input=value, paddings=space_to_batch_pad, block_size=rate) value = gen_nn_ops.conv2d(input=value, filter=filters, strides=[1, 1, 1, 1], padding="VALID", name=name) # The crops argument to batch_to_space is just the extra padding component v = array_ops.expand_dims(array_ops.pack([0, pad_bottom_extra]),1) h = array_ops.expand_dims(array_ops.pack([0, pad_right_extra]),1) batch_to_space_crop = array_ops.concat(1, [v,h]) batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]] value = array_ops.batch_to_space(input=value, crops=batch_to_space_crop, block_size=rate) return value