Applying a 1-channel mask to a 3-channel tensor in a tensor flow

I am trying to apply a mask (binary, only one channel) to an RGB image (3 channels normalized to [0, 1]). My current solution is that I split the RGB image into it, multiply it by the mask, and combine these channels again:

with tf.variable_scope('apply_mask') as scope:
  # Output mask is in range [-1, 1], bring to range [0, 1] first
  zero_one_mask = (output_mask + 1) / 2
  # Apply mask to all channels.
  channels = tf.split(3, 3, output_img)
  channels = [tf.mul(c, zero_one_mask) for c in channels]
  output_img = tf.concat(3, channels)

However, this seems rather inefficient, especially since, in my opinion, none of these calculations are performed in place. Is there a more efficient way to do this?

+3
source share
1 answer

The operator tf.mul()supports numpy-style broadcasting , which will simplify and optimize the code a bit.

, zero_one_mask m x n, output_img b x m x n x 3 ( b - - , output_img 3) *. tf.expand_dims(), zero_one_mask channels, m x n x 1:

with tf.variable_scope('apply_mask') as scope:
  # Output mask is in range [-1, 1], bring to range [0, 1] first
  # NOTE: Assumes `output_mask` is a 2-D `m x n` tensor.
  zero_one_mask = tf.expand_dims((output_mask + 1) / 2, 2)
  # Apply mask to all channels.
  # NOTE: Assumes `output_img` is a 4-D `b x m x n x c` tensor.
  output_img = tf.mul(output_img, zero_one_mask)

(* , output_img 4-D b x m x n x c ( c) 3-D m x n x c , - , .)

+5

All Articles