Missed TensorFlow Dev Summit? Check out the video playlist. Watch recordings


View source on GitHub

Rearrange image blocks into rows.

    x, block_shape, slice_step=(1, 1), data_format='NHWC', padding='VALID',

This function can be used to implement 2D convolution as a matml, e.g.,

tf.nn.conv2d(x, k) = tf.matmul(im2row(x), tf.reshape(k, [-1, out_size])).


  • x: Rank 3 (or more) Tensor representing 2D images.
  • block_shape: Length-2 vector representing the block or "filter" shape.
  • slice_step: Length-2 vector specifying the convolution stride length. Default value: (1, 1).
  • data_format: One of 'NHWC' or 'NCHW' (case insensitive). Default value: 'NHWC'.
  • padding: One of 'VALID' or 'SAME' (case insensitive). Default value: 'VALID'.
  • name: Python str used to describe ops created by this function. Default value: None (i.e., 'im2col').


  • im2row_x: batch of matrices representing subblock copies of x. Same batch shape as x but with rightmost shape: batch_shape + [oh * ow, block_shape[0] * block_shape[1] * channels], where oh = (h - block_shape[0] + 1) // slice_step[0] and ow = (w - block_shape[1] + 1) // slice_step[1] when padding = 'VALID' and oh = h and ow = w when padding = 'SAME'.
  • shape: shape Tensor equivalent to: batch_shape + [oh, ow, block_shape[0] * block_shape[1] * channels] where oh, ow are defined as above.