Форма должна иметь ранг 0, но ранг 1 для '{{node ReadFile}} = ReadFile [] (args_0)' с входными формами: [?] - PullRequest
0 голосов
/ 13 июля 2020
ValueError: Shape must be rank 0 but is rank 1 for '{{node ReadFile}} = ReadFile[](args_0)' with input shapes: [?].

получил следующую ошибку tenorflow 2.0

import tensorflow as tf

    def preprocessing(image):
        print('preprocessing->',image)
        image = tf.io.read_file(image)
        #image = tf.image.decode_jpeg(img, channels=3)
        #image = tf.cast(img, tf.float32)
        return image
    
    def train(ds):
        for i, batch in enumerate(ds):
            print("=====================batch{}====================={}".format(i,batch))
            for x in batch:
                print(x)
                #print(x.numpy())
                #print(x[1].numpy())

images = ['/media/jake/mark-4tb3/Screenshot_from_2020-02-19_23-05-22.png','/media/jake/mark-4tb3/Screenshot_from_2020-02-19_23-05-22.png','/media/jake/mark-4tb3/Screenshot_from_2020-02-19_23-05-22.png','/media/jake/mark-4tb3/Screenshot_from_2020-02-19_23-05-22.png']

images = tf.ragged.constant(images)

dataset = tf.data.Dataset.from_tensor_slices((images))
train(dataset.batch(2).map(preprocessing))

тогда вся ошибка

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-159-9b4a28882f72> in <module>
      1 dataset = tf.data.Dataset.from_tensor_slices((images))
----> 2 train(dataset.batch(2).map(preprocessing))

~/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py in map(self, map_func, num_parallel_calls, deterministic)
   1619     """
   1620     if num_parallel_calls is None:
-> 1621       return MapDataset(self, map_func, preserve_cardinality=True)
   1622     else:
   1623       return ParallelMapDataset(

~/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py in __init__(self, input_dataset, map_func, use_inter_op_parallelism, preserve_cardinality, use_legacy_function)
   3979         self._transformation_name(),
   3980         dataset=input_dataset,
-> 3981         use_legacy_function=use_legacy_function)
   3982     variant_tensor = gen_dataset_ops.map_dataset(
   3983         input_dataset._variant_tensor,  # pylint: disable=protected-access

~/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py in __init__(self, func, transformation_name, dataset, input_classes, input_shapes, input_types, input_structure, add_to_graph, use_legacy_function, defun_kwargs)
   3219       with tracking.resource_tracker_scope(resource_tracker):
   3220         # TODO(b/141462134): Switch to using garbage collection.
-> 3221         self._function = wrapper_fn.get_concrete_function()
   3222 
   3223         if add_to_graph:

~/venv/lib/python3.7/site-packages/tensorflow/python/eager/function.py in get_concrete_function(self, *args, **kwargs)
   2530     """
   2531     graph_function = self._get_concrete_function_garbage_collected(
-> 2532         *args, **kwargs)
   2533     graph_function._garbage_collector.release()  # pylint: disable=protected-access
   2534     return graph_function

~/venv/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_garbage_collected(self, *args, **kwargs)
   2494       args, kwargs = None, None
   2495     with self._lock:
-> 2496       graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
   2497       if self.input_signature:
   2498         args = self.input_signature

~/venv/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   2775 
   2776       self._function_cache.missed.add(call_context_key)
-> 2777       graph_function = self._create_graph_function(args, kwargs)
   2778       self._function_cache.primary[cache_key] = graph_function
   2779       return graph_function, args, kwargs

~/venv/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2665             arg_names=arg_names,
   2666             override_flat_arg_shapes=override_flat_arg_shapes,
-> 2667             capture_by_value=self._capture_by_value),
   2668         self._function_attributes,
   2669         # Tell the ConcreteFunction to clean up its graph once it goes out of

~/venv/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    979         _, original_func = tf_decorator.unwrap(python_func)
    980 
--> 981       func_outputs = python_func(*func_args, **func_kwargs)
    982 
    983       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py in wrapper_fn(*args)
   3212           attributes=defun_kwargs)
   3213       def wrapper_fn(*args):  # pylint: disable=missing-docstring
-> 3214         ret = _wrapper_helper(*args)
   3215         ret = structure.to_tensor_list(self._output_structure, ret)
   3216         return [ops.convert_to_tensor(t) for t in ret]

~/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py in _wrapper_helper(*args)
   3154         nested_args = (nested_args,)
   3155 
-> 3156       ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
   3157       # If `func` returns a list of tensors, `nest.flatten()` and
   3158       # `ops.convert_to_tensor()` would conspire to attempt to stack

~/venv/lib/python3.7/site-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    263       except Exception as e:  # pylint:disable=broad-except
    264         if hasattr(e, 'ag_error_metadata'):
--> 265           raise e.ag_error_metadata.to_exception(e)
    266         else:
    267           raise

ValueError: in user code:

    <ipython-input-156-6db31559ad89>:5 preprocessing  *
        image = tf.io.read_file(image)
    /home/jake/venv/lib/python3.7/site-packages/tensorflow/python/ops/gen_io_ops.py:568 read_file  **
        "ReadFile", filename=filename, name=name)
    /home/jake/venv/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:744 _apply_op_helper
        attrs=attr_protos, op_def=op_def)
    /home/jake/venv/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py:595 _create_op_internal
        compute_device)
    /home/jake/venv/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:3327 _create_op_internal
        op_def=op_def)
    /home/jake/venv/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:1817 __init__
        control_input_ops, op_def)
    /home/jake/venv/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:1657 _create_c_op
        raise ValueError(str(e))

    ValueError: Shape must be rank 0 but is rank 1 for '{{node ReadFile}} = ReadFile[](args_0)' with input shapes: [?].
...