0

I have a batches of images represented as TensorFlow tensor (say Tensor 1) of shape (2,1024,1024,1) of the form (B,H,W,C) where B is the batch size 2, H and W are image dimensions 1024 and C is the number of channels. Each element of this tensor (i.e. each pixel) stores a tuple (a,b) where both a and b are in the range 0 and 255.

I have another tensor (say Tensor 2) of shape (256,256) for which each element stores a single value between 0 and 255.

Given this setup, I have the following question.

I wish to replace each element value in Tensor 1 with the corresponding element value in Tensor 2. For example, let's assume that the element given by index (1,200,500,1) in tensor 1 contains the value (100,20). I want to look up the value stored in Tensor 2 at pixel location (100,200) and modify the entry at (1,200,500,1) with this value.

How can I do this in the most efficient way possible for the entire batch?

Please let me know if something is unclear. I am a beginner to TensorFlow, hence would appreciate any help.

1 Answer 1

0

to replace an element value in Tensor 1 with a corresponding element value in Tensor 2 try this way

tensor1 = tf.Variable(tf.zeros((2,1024,1024,1)), trainable=False)
tensor2 = tf.constant(np.random.randint(0,255, (255,255,1)), dtype='float32')

tensor12 = tensor1[1,200,500].assign(tensor2[100,200])

in this example, you substituted the element in position (1,200,500,1) of Tensor 1 with the element in position (100,200) of Tensor 2


batch_dim = 2
tensor1 = tf.Variable(tf.zeros((batch_dim,1024,1024,1)), trainable=False)
tensor2 = tf.constant(np.random.randint(0,255, (1,255,255,1)), dtype='float32')

tensor12 = tensor1[:,:tensor2.shape[1],:tensor2.shape[2]].assign(tf.repeat(tensor2, batch_dim, axis=0))

with these lines, you assign all the value of tensor2 to the first (255,255) position in tensor1 for ALL the samples of the batch

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.