slice instead of convs in crop layer
@@ -24,19 +24,7 @@ class CropLayer(Layer):
def layer_op(self, inputs):
spatial_rank = layer_util.infer_spatial_rank(inputs)
- kernel_shape = np.hstack((
- [self.border * 2 + 1] * spatial_rank, 1, 1)).flatten()
- # initializer a kernel with all 0s, and set the central element to 1
- np_kernel = layer_util.trivial_kernel(kernel_shape)
- crop_kernel = tf.constant(np_kernel, dtype=inputs.dtype)
- # split channel dim
- output_tensor = [tf.expand_dims(x, -1)
- for x in tf.unstack(inputs, axis=-1)]
- output_tensor = [tf.nn.convolution(input=inputs,
- filter=crop_kernel,
- strides=[1] * spatial_rank,
- padding='VALID',
- name='conv')
- for inputs in output_tensor]
- output_tensor = tf.concat(output_tensor, axis=-1)
+ offsets = [0, *([int(self.border)] * spatial_rank), 0]
+ out_shape = [-1, *[int(d) - 2 * int(self.border) for d in list(inputs.shape)[1:-1]], -1]
+ output_tensor = tf.slice(inputs, offsets, out_shape)
return output_tensor
