Я пытаюсь создать конвейер tf.data
, который принимает пути к изображениям в качестве входных данных и возвращает функции VGG16
в качестве выходных данных. input_1
- список, содержащий пути к изображениям. Вот код:
def vgg_pred(image):
model = VGG16()
model = Model(inputs=model.inputs, outputs=model.layers[-5].output)
feature = model.predict(image)
return feature
def extract_features(filename):
image = load_img(filename.numpy(), target_size=(224, 224))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)
feature = vgg_pred(image)
feature = feature.reshape(7,7,512)
return feature
buffer_size = 40
train_dataset = tf.data.Dataset.from_tensor_slices((input_1))
train_dataset = train_dataset.map(lambda x: tf.py_function(extract_features,[x],tf.float32))
Если я перебираю набор данных следующим образом:
for i in train_dataset.take(1):
print(i)
Я получаю эту ошибку:
---------------------------------------------------------------------------
UnknownError Traceback (most recent call last)
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\context.py in execution_mode(mode)
1896 ctx.executor = executor_new
-> 1897 yield
1898 finally:
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\data\ops\iterator_ops.py in _next_internal(self)
658 output_types=self._flat_output_types,
--> 659 output_shapes=self._flat_output_shapes)
660
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\gen_dataset_ops.py in iterator_get_next_sync(iterator, output_types, output_shapes, name)
2477 except _core._NotOkStatusException as e:
-> 2478 _ops.raise_from_not_ok_status(e, name)
2479 # Add nodes to the TensorFlow graph.
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\framework\ops.py in raise_from_not_ok_status(e, name)
6605 # pylint: disable=protected-access
-> 6606 six.raise_from(core._status_to_exception(e.code, message), None)
6607 # pylint: enable=protected-access
~\anaconda3\envs\plant\lib\site-packages\six.py in raise_from(value, from_value)
UnknownError: LookupError: No gradient defined for operation 'IteratorGetNext' (op type: IteratorGetNext)
Traceback (most recent call last):
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\gradients_util.py", line 607, in _GradientsHelper
grad_fn = ops.get_gradient_function(op)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\framework\ops.py", line 2495, in get_gradient_function
return _gradient_registry.lookup(op_type)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\framework\registry.py", line 97, in lookup
"%s registry has no entry for: %s" % (self._name, name))
LookupError: gradient registry has no entry for: IteratorGetNext
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\script_ops.py", line 234, in __call__
return func(device, token, args)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\script_ops.py", line 123, in __call__
ret = self._func(*args)
File "<ipython-input-4-7f2cad24e305>", line 12, in extract_features
feature = vgg_pred(image)
File "<ipython-input-4-7f2cad24e305>", line 4, in vgg_pred
feature = model.predict(image)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1013, in predict
use_multiprocessing=use_multiprocessing)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 498, in predict
workers=workers, use_multiprocessing=use_multiprocessing, **kwargs)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 475, in _model_iteration
total_epochs=1)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 128, in run_one_epoch
batch_outs = execution_function(iterator)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 98, in execution_function
distributed_function(input_fn))
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 568, in __call__
result = self._call(*args, **kwds)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 638, in _call
return self._concrete_stateful_fn._filtered_call(canon_args, canon_kwds) # pylint: disable=protected-access
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1611, in _filtered_call
self.captured_inputs)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1697, in _call_flat
forward_function, args_with_tangents = forward_backward.forward()
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1423, in forward
self._inference_args, self._input_tangents)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1185, in forward
self._forward_and_backward_functions(inference_args, input_tangents))
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1331, in _forward_and_backward_functions
outputs, inference_args, input_tangents)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 890, in _build_functions_for_outputs
src_graph=self._func_graph)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\gradients_util.py", line 623, in _GradientsHelper
(op.name, op.type))
LookupError: No gradient defined for operation 'IteratorGetNext' (op type: IteratorGetNext)
[[{{node EagerPyFunc}}]] [Op:IteratorGetNextSync]
During handling of the above exception, another exception occurred:
UnknownError Traceback (most recent call last)
<ipython-input-5-963f6e7cd6bb> in <module>
----> 1 for i in train_dataset.take(1):
2 print(i)
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\data\ops\iterator_ops.py in __next__(self)
628
629 def __next__(self): # For Python 3 compatibility
--> 630 return self.next()
631
632 def _next_internal(self):
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\data\ops\iterator_ops.py in next(self)
672 """Returns a nested structure of `Tensor`s containing the next element."""
673 try:
--> 674 return self._next_internal()
675 except errors.OutOfRangeError:
676 raise StopIteration
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\data\ops\iterator_ops.py in _next_internal(self)
663 return self._element_spec._from_compatible_tensor_list(ret) # pylint: disable=protected-access
664 except AttributeError:
--> 665 return structure.from_compatible_tensor_list(self._element_spec, ret)
666
667 @property
~\anaconda3\envs\plant\lib\contextlib.py in __exit__(self, type, value, traceback)
128 value = type()
129 try:
--> 130 self.gen.throw(type, value, traceback)
131 except StopIteration as exc:
132 # Suppress StopIteration *unless* it's the same exception that
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\context.py in execution_mode(mode)
1898 finally:
1899 ctx.executor = executor_old
-> 1900 executor_new.wait()
1901
1902
~\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\executor.py in wait(self)
65 def wait(self):
66 """Waits for ops dispatched in this executor to finish."""
---> 67 pywrap_tensorflow.TFE_ExecutorWaitForAllPendingNodes(self._handle)
68
69 def clear_error(self):
UnknownError: LookupError: No gradient defined for operation 'IteratorGetNext' (op type: IteratorGetNext)
Traceback (most recent call last):
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\gradients_util.py", line 607, in _GradientsHelper
grad_fn = ops.get_gradient_function(op)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\framework\ops.py", line 2495, in get_gradient_function
return _gradient_registry.lookup(op_type)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\framework\registry.py", line 97, in lookup
"%s registry has no entry for: %s" % (self._name, name))
LookupError: gradient registry has no entry for: IteratorGetNext
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\script_ops.py", line 234, in __call__
return func(device, token, args)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\script_ops.py", line 123, in __call__
ret = self._func(*args)
File "<ipython-input-4-7f2cad24e305>", line 12, in extract_features
feature = vgg_pred(image)
File "<ipython-input-4-7f2cad24e305>", line 4, in vgg_pred
feature = model.predict(image)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1013, in predict
use_multiprocessing=use_multiprocessing)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 498, in predict
workers=workers, use_multiprocessing=use_multiprocessing, **kwargs)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 475, in _model_iteration
total_epochs=1)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 128, in run_one_epoch
batch_outs = execution_function(iterator)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 98, in execution_function
distributed_function(input_fn))
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 568, in __call__
result = self._call(*args, **kwds)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 638, in _call
return self._concrete_stateful_fn._filtered_call(canon_args, canon_kwds) # pylint: disable=protected-access
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1611, in _filtered_call
self.captured_inputs)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1697, in _call_flat
forward_function, args_with_tangents = forward_backward.forward()
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1423, in forward
self._inference_args, self._input_tangents)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1185, in forward
self._forward_and_backward_functions(inference_args, input_tangents))
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 1331, in _forward_and_backward_functions
outputs, inference_args, input_tangents)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\eager\function.py", line 890, in _build_functions_for_outputs
src_graph=self._func_graph)
File "C:\Users\divinity\anaconda3\envs\plant\lib\site-packages\tensorflow_core\python\ops\gradients_util.py", line 623, in _GradientsHelper
(op.name, op.type))
LookupError: No gradient defined for operation 'IteratorGetNext' (op type: IteratorGetNext)
[[{{node EagerPyFunc}}]]
Насколько я проверено, ошибка возникает во время model.predict
. Есть ли другой способ получить функции VGG, используя tf.data
? Любая помощь будет отличной!