Tensorflow 2 throwing ValueError: as_list() is not defined on an unknown TensorShape

I had the same problem as you with image and mask and solved it by setting both their shapes during the preprocessing function manually, in particular when calling a pyfunc during the tf.map.

def process_path(filePath):
  ...

  # load the raw data from the file as a string
  img = tf.io.read_file(filePath)
  img = decode_img(img)
  mask = tf.py_function(decode_npy, [maskPath], tf.float32)

  # TODO:
  img.set_shape([MANUALLY ENTER THIS])
  mask.set_shape([MANUALLY ENTER THIS])

  return img, mask

I had a similar issue and used dataset.padded_batch with explicit padded_shapes solved my problem!