Indexing in TensorFlow

I would like to do the following code in TensorFlow,

Blockquote

masking[ymin:ymax,xmin:xmax] = img[ymin:ymax,xmin:xmax]

Blockquote

The above code can’t work in TensorFlow. I have checked the “tf.tensor_scatter_nd_update” method but that work on some indices not doing extraction type work, basically defining the range of indices.

Hi,

You can definitely index into a tensor like this:

import tensorflow as tf

masking = tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=tf.float32)

y_min = 0
y_max = 2
x_min = 1
x_max = 3
masking[y_min:y_max, x_min:x_max]

Output:

<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2., 3.],
       [6., 7.]], dtype=float32)>

What you cannot do is indeed tensor assignment:

img = tf.constant([[9, 10, 11, 12], [13, 14, 15, 16]], dtype=tf.float32)

masking[y_min:y_max, x_min:x_max] = img[y_min:y_max, x_min:x_max]

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-482e1a48b3c5> in <cell line: 3>()
      1 img = tf.constant([[9, 10, 11, 12], [13, 14, 15, 16]], dtype=tf.float32)
      2 
----> 3 masking[y_min:y_max, x_min:x_max] = img[y_min:y_max, x_min:x_max]

TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment

This is because in TensorFlow tensors are immutable.

What you can do instead is wrap your tensors in Variables:

masking = tf.Variable(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=tf.float32))
img = tf.Variable(tf.constant([[9, 10, 11, 12], [13, 14, 15, 16]], dtype=tf.float32))

masking[y_min:y_max, x_min:x_max].assign(img[y_min:y_max, x_min:x_max])

Output:

<tf.Variable 'UnreadVariable' shape=(2, 4) dtype=float32, numpy=
array([[ 1., 10., 11.,  4.],
       [ 5., 14., 15.,  8.]], dtype=float32)>

These allow the in place updates on sliced tensors.

"TypeError: in user code:

File "/tmp/ipykernel_32/3325902878.py", line 25, in read_image_tfds  *
    kernel = tf.Variable(tf.constant(img[y_min:y_max,x_min:x_max,:]))

TypeError: Expected any non-tensor type, but got a tensor instead."

@Abdur_Rehman_Khan

In this case, img is already a tensor, so no need to wrap it in tf.constant.

" ValueError: Argument initial_value (Tensor(“strided_slice:0”, shape=(41, 123, 1), dtype=int32)) could not be lifted out of a tf.function. (Tried to create variable with name=‘None’). To avoid this error, when constructing tf.Variables inside of tf.function you can create the initial_value tensor in a tf.init_scope or pass a callable initial_value (e.g., tf.Variable(lambda : tf.truncated_normal([10, 40]))). Please file a feature request if this restriction inconveniences you."

My Code,
"def read_image_tfds(image, label,ymin,xmin,ymax,xmax,img_w,img_h,shape=224):
w_f = shape/img_w
h_f = shape/img_h
ymin = tf.cast(ymin, tf.float64)
xmin = tf.cast(xmin, tf.float64)
ymax = tf.cast(ymax, tf.float64)
xmax = tf.cast(xmax, tf.float64)
ymin = yminw_f
xmin = xmin
h_f
ymax = ymaxw_f
xmax = xmax
h_f
ymin = tf.cast(ymin, tf.int64)
xmin = tf.cast(xmin, tf.int64)
ymax = tf.cast(ymax, tf.int64)
xmax = tf.cast(xmax, tf.int64)
read_img =tf.io.read_file(image)
img = tf.io.decode_jpeg(read_img)
#print (img.shape)
img= tf.image.resize(img, (shape, shape),method=‘nearest’)
img = tf.reshape(img,(img.shape[0],img.shape[1],3))
img = tf.cast(img,tf.int32)
#img = tfclahe(img)
img = tf.image.rgb_to_grayscale(img)
mask = tf.Variable(np.zeros((img.shape)),validate_shape=False, dtype=tf.int32)
#numpy_array = tf.get_static_value(img[y_min:y_max,x_min:x_max,:],partial=True)
#print(type(numpy_array))
kernel = tf.Variable(img[ymin:ymax,xmin:xmax,:], validate_shape=False,
name=None,dtype=tf.int32)
#print(type(kernel))
mask = mask[ymin:ymax,xmin:xmax,:].assign(kernel)
mask = tf.image.grayscale_to_rgb(mask)
return mask
"
This code works fine outside tf.data

That code works for me now,

mask = tf.Variable(np.zeros((img.get_shape())), dtype=tf.int32)
mask = mask[ymin:ymax,xmin:xmax,:].assign(img[ymin:ymax,xmin:xmax,:])

Note:- img is a tensor acquired through reading the image file.