【发布时间】:2020-05-16 05:54:41
【问题描述】:
我第一次尝试使用tf.data API with help from this example,我有 3D 体积数据,即不是(高度、宽度、通道),而是(深度、高度、宽度、通道) .
def readfile(filenames):
name = filenames[0]
string = tf.read_file(name)
image = tf.image.decode_image(string, channels=3)
bunch = image
for name in filenames[1:]:
string = tf.read_file(name)
image = tf.image.decode_image(string, channels=3)
bunch = tf.concat([bunch,image],1)
return bunch
with tf.device("/cpu:0"):
#read data file paths, shape [5,100] (five elements each with 100 frames)
train_dataset = tf.data.Dataset.from_tensor_slices(train_files)
#train_dataset.element_spec gives shape=(100,)
train_dataset = train_dataset.map(readfile, num_parallel_calls=16)
#readfile function takes element of shape (1,100) and
#reads each frame and appends to a tensor which is returned
#train_dataset.element_spec gives shape=<unknown>
train_dataset = train_dataset.map(lambda x: tf.random_crop(x, (100, 256, 256, 3)))
#train_dataset.element_spec gives shape=(100, 256, 256, 3)
train_dataset = train_dataset.batch(1)
x = train_dataset.make_one_shot_iterator().get_next()
错误:
Traceback (most recent call last):
File "/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1365, in _do_call
return fn(*args)
File "/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1350, in _run_fn
target_list, run_metadata)
File "/anaconda3/envs/myenv/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1443, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: {{function_node __inference_Dataset_map_<lambda>_258}} Incompatible shapes: [3] vs. [4]
[[{{node random_crop/GreaterEqual}}]]
[[IteratorGetNext]]
我无法理解错误。我认为这意味着tf.random_crop 给出了 3D 张量形状,而.get_next() 部分给出了 4D 张量?在应用readfile 函数后,我对train_dataset 的形状有疑问,为什么是<unknown> 的形状,我希望类似于 (?,100,256,256,3)。我哪里错了?
有没有办法可视化 train_dataset 中的帧,所以我知道我做对了?我一直使用feed_dict,在那里很容易看到 numpy 帧,所以我确切地知道我在喂什么。
【问题讨论】:
-
你能发布readfile函数吗?另外,让它接受形状为 (100,) 而不是 (1,100) 的元素。我的猜测是,如果您没有在函数内部将张量重新整形为 (100,1),它根本不会读取任何数据
-
@MiloMinderbinder 我已经更新了 readfile 函数代码。这个函数的输入似乎是一维的,这就是我这样写的原因。当我尝试其他一些变化时,它给了我错误
ValueError: Index out of range using input dim 1; input has only 1 dims for 'strided_slice' (op: 'StridedSlice') with input shapes: [100], [2], [2], [2] and with computed input tensors: input[3] = <1 1>.
标签: python tensorflow