0

I want to concatenate three images with size [1024,1024,3] to make a batch with size [3,1024,1024,3]. I wrote this code with TensorFlow but it doesn't work. It returns the error "InaccessibleTensorError: The tensor 'Tensor("truediv:0", shape=(1024, 1024, 3), dtype=float32)' cannot be accessed here: it is defined in another function or code block. Use return values, explicit Python locals or TensorFlow collections to access it.".

def decode_img(filename):
    image = tf.ones((3,1024,1024,3),dtype=tf.dtypes.float32)
    cnt=0
    slices = []
    for fi in filename:
      bits = tf.io.read_file(fi)
      img = tf.image.decode_jpeg(bits, channels=3)
      img = tf.image.resize(img, (1024,1024))
      slices.append(tf.cast(img, tf.float32) / 255.0)
      cnt +=1

    image = tf.stack(slices)
    return image

#-----------------------
filenames = ['img1.png', 'img2.png', 'img3.png']
dataset = tf.data.Dataset.from_tensor_slices(filenames)
dataset = dataset.map(decode_img, num_parallel_calls=AUTO)
1

1 Answer 1

1

In general, tensorflow does not support item assignment. Rather, generate all the img layers you want and then use tf.stack() or tf.concatenate.

filename = [img1.png, img2.png, img3.png]
cnt=0
slices = []
for fi in filename:
  bits = tf.io.read_file(fi)
  img = tf.image.decode_jpeg(bits, channels=3)
  img = tf.image.resize(img, (1024,1024))
  slices.append(tf.cast(img, tf.float32) / 255.0)
  cnt +=1

image = tf.stack(slices)
Sign up to request clarification or add additional context in comments.

6 Comments

By this way, it returns error "InaccessibleTensorError: The tensor 'Tensor("truediv:0", shape=(1024, 1024, 3), dtype=float32)' cannot be accessed here: it is defined in another function or code block. Use return values, explicit Python locals or TensorFlow collections to access it"
Actually, I am using the following code, it returns the error on line "image = tf.stack(slices)" def decode_img(filename): image = tf.ones((3,1024,1024,3),dtype=tf.dtypes.float32) cnt=0 slices = [] for fi in filename: bits = tf.io.read_file(fi) img = tf.image.decode_jpeg(bits, channels=3) img = tf.image.resize(img, (1024,1024)) slices.append(tf.cast(img, tf.float32) / 255.0) cnt +=1 image = tf.stack(slices) return image filenames = [img1.png, img2.png, img3.png] dataset = tf.data.Dataset.from_tensor_slices(filenames) dataset = dataset.map(decode_img)
Post in question formatted as code, this is very hard to read
See, in your code you define image first, then try to assign a new value to image. Assignment is not allowed in general in tf. Every time you want to combine a few variables or modify them, you need to assign the result to a new variable. This is based on the way tensorflow defines its function graphs for efficient backpropogation I believe
sorry, that was a piece of my code, I edited the question and put the all of my code
|

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.