Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: tf.pad to pad an image with different values correspond to different channels respectively #19193

Open
Kongsea opened this issue May 10, 2018 · 7 comments

Comments

@Kongsea
Copy link
Contributor

commented May 10, 2018

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04
  • TensorFlow installed from (source): source
  • TensorFlow version (use command below): 1.4.1
  • Python version: 2.7
  • CUDA/cuDNN version: 8.0
  • GPU model and memory: GTX 1080, 8G

Describe the problem

tf.pad has a parameter constant_values to control the padding values. However, the value need to be a single scalar and it is used to pad all the channels equally.
Then if we want to pad the imagenet mean values [123.68, 116.779, 103.939] to 3 channels of a RGB image tensor respectively, we cannot achieve it by tf.pad.

So is it possible to extend tf.pad to be able to pad 3 channels with different values respectively?

@tensorflowbutler

This comment has been minimized.

Copy link
Member

commented May 10, 2018

Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks.
Bazel version
Exact command to reproduce

@tokotchd

This comment has been minimized.

Copy link

commented May 10, 2018

You can do this with three separate padding operations and a concat.

I.E. if an example "yourTensor" has 4 dimensions (batch,width,height,channels)

paddedR = tf.pad(tensor = yourTensor[:,:,:,0:1], #the R channel of tensor
    paddings=[[0,0],[2,2],[2,2],[0,0]],  #don't pad it in batch or channel dimensions
    mode='Constant',
    name='padAverageRed',
    constant_values=123.68)
paddedG = tf.pad(tensor = yourTensor[:,:,:,1:2], #the G channel of tensor
    paddings=[[0,0],[2,2],[2,2],[0,0]],  #don't pad it in batch or channel dimensions
    mode='Constant',
    name='padAverageGreen',
    constant_values=116.779)
paddedB = tf.pad(tensor = yourTensor[:,:,:,2:3], #the B channel of tensor
    paddings=[[0,0],[2,2],[2,2],[0,0]], #don't pad it in batch or channel dimensions
    mode='Constant',
    name='padAverageBlue',
    constant_values=103.939)
finalTensor=tf.concat([paddedR, paddedG, paddedB], 3)

You can replace the slices on the final dimension with 0,1,and 2 respectively and use stack instead of concat and you will be able to just use a constant padding of "2" across every dimension which neatens the code up a decent bit but it also makes it less understandable. Either will be sufficient, however.

@Kongsea

This comment has been minimized.

Copy link
Contributor Author

commented May 11, 2018

@tokotchd Thank you.
I have also implemented it before.
However, I still think it will be better to extend the function tf.pad.

@Hadjimina

This comment has been minimized.

Copy link

commented May 13, 2018

@tokotchd Nice snippet! However, you missed the commas after name='padAverageGreen' and name='padAverageBlue'. It would be great if you could update the code with this minor change.

@tokotchd

This comment has been minimized.

Copy link

commented May 15, 2018

@Hadjimina updated snippet

@cy89 cy89 added the type:feature label Jul 21, 2018

@cy89

This comment has been minimized.

Copy link

commented Jul 21, 2018

@rryan might this kind of generalization of the pad value be planned, or should I mark "contributions welcome"?

@rryan

This comment has been minimized.

Copy link
Member

commented Aug 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.