Tensorflow 2.0 InaccessibleTensorError

Question:

So I’m trying to load Array Data from TFRecords. But I’m getting a Error Message while trying to parse the file Path into the Array.

If I just call t= functions.parse_TFR_to_tens(filenames) everything works out fine, but if it is being called inside of process_path the error message appears.
I have no clue what is going on, and couldn’t fix the problem. after all the Function in itself works, so what am I missing?

Funktions:

def _parse_image_function(example_proto):
    image_feature_description = {

    'feature3': tf.io.FixedLenFeature([], tf.string),
    }

    return tf.io.parse_single_example(example_proto, image_feature_description)

def parse_TFR_to_tens(file_name):
    raw_dataset = tf.data.TFRecordDataset(file_name)
    parsed_image_dataset = raw_dataset.map(_parse_image_function)
    s = []
    for image_features in parsed_image_dataset:
        image_raw = tf.io.parse_tensor(image_features['feature3'], out_type=tf.float64)
        s.append(image_raw)
    t = tf.stack(s)
    return t

def process_path(file_path,verbose=False):
  parts = tf.strings.split(file_path, '/')
  label = 0
  print(parts[-2])

  pd = settings.pd
  l = 0
  for p in pd:
      if parts[-2] == p:
          label = 1
  #return tf.io.read_file(file_path), label
  t = parse_TFR_to_tens(file_path)
  return t, label

def create_ds(sets=[True,True,True], snaps=3, mode=1,verbose=False):
    sets = settings.settings["sets"]
    snaps= settings.settings["snaps"]
    mode = settings.settings["mode"]
    verbose = settings.settings["verbose"]
    ds=[]
    if sets[0]:
        path = globstringPET(sets=sets,snaps=snaps,mode=mode)
        list_ds = tf.data.Dataset.list_files(path)
        labeled_ds_PET = list_ds.map(process_path)
        ds.append(labeled_ds_PET)
    if sets[1]:
        path = globstringMRT(sets=sets, snaps=snaps, mode=mode)
        list_ds = tf.data.Dataset.list_files(path)
        labeled_ds_MRT = list_ds.map(process_path)
        ds.append(labeled_ds_MRT)
    if verbose: print("Sets: ",ds)
    first = True
    for d in ds:
        if first:
            labeled_ds = d
            first = False
        else:
            labeled_ds.concatenate(d)

    return labeled_ds, list_ds

The Error Message:

    /misc/no_backup/d1325/CNNMEL/functions.py:257 process_path  *
    t = parse_TFR_to_tens(file_path)
/misc/no_backup/d1325/CNNMEL/functions.py:144 parse_TFR_to_tens  *
    t = tf.stack(s)
/no_backup/d1325/.local/lib/python3.6/site-packages/tensorflow_core/python/util/dispatch.py:180 wrapper
    return target(*args, **kwargs)
/no_backup/d1325/.local/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py:1165 stack
    return gen_array_ops.pack(values, axis=axis, name=name)
/no_backup/d1325/.local/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py:6304 pack
    "Pack", values=values, axis=axis, name=name)
/no_backup/d1325/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py:793 _apply_op_helper
    op_def=op_def)
/no_backup/d1325/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py:544 create_op
    inp = self.capture(inp)
/no_backup/d1325/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py:603 capture
    % (tensor, tensor.graph, self))

InaccessibleTensorError: The tensor 'Tensor("ParseTensor:0", dtype=float64)' 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. Defined in: FuncGraph(name=reduce_reduce_body, id=139628601725840); accessed from: FuncGraph(name=Dataset_map_process_path, id=139630684432872).
Asked By: C.A.M.R

||

Answers:

The Mistake was not adding a @tf.function Decorator to the Process_path Function. With that everything worked out fine.

Answered By: C.A.M.R
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.