我在Tensorflow数据导入管道中有一个内部函数,我试图在其中应用各向异性扩散。我目前被一个IndexError: only integers, slices (":"), ellipsis ("..."), numpy.newaxis ("None") and integer or boolean arrays are valid indices
卡住了,我无法理解实际问题。情况如下:
deltas = [np.zeros(out.shape) for _ in range(out.shape.rank)]
for _ in range(niter):
for i in range(out.shape.rank):
slicer =[slice(None, -1) if j == i else slice(None) for j in range(out.shape.rank)]
deltas[i][slicer] = tf.experimental.numpy.diff(out, axis=i)
'out'是具有形状[224,224,1]的3D tfTensor;
我已经尝试了不同的解决方案,比如使用元组和numpy.s_代替slice函数,但没有成功。为了清晰起见,这段代码位于www.example.com()中的lambda调用的函数中tf.Dataset.map。预期的输出将能够访问正确的切片。
完整的错误如下:
Traceback (most recent call last):
File "/Users/leo/Documents/repos/ChestXRayEnsembling/ChestXRAY/multidecoder/train_script.py", line 83, in <module>
ds_d = dlo.XRayDatasetFromDirs(
File "/Users/leo/Documents/repos/ChestXRayEnsembling/ChestXRAY/multidecoder/myDataLoaders.py", line 236, in XRayDatasetFromDirs
ds = ds.map(lambda *args: (f(args[0], **kwargs), *args[1:]),
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2296, in map
return ParallelMapDataset(
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 5540, in __init__
self._map_func = structured_function.StructuredFunctionWrapper(
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/data/ops/structured_function.py", line 263, in __init__
self._function = fn_factory()
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 226, in get_concrete_function
concrete_function = self._get_concrete_function_garbage_collected(
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 192, in _get_concrete_function_garbage_collected
concrete_function, _ = self._maybe_define_concrete_function(args, kwargs)
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 157, in _maybe_define_concrete_function
return self._maybe_define_function(args, kwargs)
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 360, in _maybe_define_function
concrete_function = self._create_concrete_function(args, kwargs)
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 284, in _create_concrete_function
func_graph_module.func_graph_from_py_func(
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/framework/func_graph.py", line 1283, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/data/ops/structured_function.py", line 240, in wrapped_fn
ret = wrapper_helper(*args)
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/data/ops/structured_function.py", line 171, in wrapper_helper
ret = autograph.tf_convert(self._func, ag_ctx)(*nested_args)
File "/opt/homebrew/Caskroom/miniforge/base/envs/tf-metal/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py", line 692, in wrapper
raise e.ag_error_metadata.to_exception(e)
tensorflow.python.autograph.impl.api.StagingError: in user code:
File "/Users/leo/Documents/repos/ChestXRayEnsembling/ChestXRAY/multidecoder/myDataLoaders.py", line 236, in None *
lambda *args: (f(args[0], **kwargs), *args[1:])
File "/Users/leo/Documents/repos/ChestXRayEnsembling/ChestXRAY/multidecoder/myPreprocessing.py", line 413, in addChannelAnisotropicDiffusion *
diff = anisotropic_diffusion_tf(tf.expand_dims(img[:, :, 0], axis=-1), **kwargs)
File "/Users/leo/Documents/repos/ChestXRayEnsembling/ChestXRAY/multidecoder/myPreprocessing.py", line 386, in anisotropic_diffusion_tf *
deltas[i][slicer] = tf.experimental.numpy.diff(out, axis=i)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
1条答案
按热度按时间n8ghc7c11#
正如@MegaIng在评论中建议的那样,
tuple(slicer)
解决了这个问题。但是问题的原因仍然不清楚。