Home > Software design >  How to stack tensors (from images) to train a CNN?
How to stack tensors (from images) to train a CNN?

Time:08-28

I have converted images to tensors. How should I stack them to train for a Convolutional Neural Network in keras.

mask_tensor = tf.Variable([])
for img in mask_img:
    image = tf.io.read_file(img)
    tensor = tf.io.decode_jpeg(image, channels=3)
    tensor = tf.image.resize(tensor, [128,128])
    if mask_tensor.shape == 0:
        mask_tensor = tf.stack([tensor])
    else:
        tf.reshape(tensor, [1,128,128,3])
        mask_tensor = tf.stack([mask_tensor, tensor])



InvalidArgumentError: Input to reshape is a tensor with 49152 values, but the requested shape has 98304 [Op:Reshape]

CodePudding user response:

If your tensors are based on this, try :

mask_tensor = tf.TensorArray(dtype=tf.float32, size=0, dynamic_size=True)
for img in mask_img:
    image = tf.io.read_file(img)
    tensor = tf.io.decode_jpeg(image, channels=3)
    tensor = tf.image.resize(tensor, [128,128])
    mask_tensor = mask_tensor.write(mask_tensor.size(), tensor)
images = mask_tensor.stack()
images.shape
  • Related