Tensorflow替换numpy tostring

时间:2018-07-31 15:02:16

标签: python tensorflow tensorflow-datasets

我正在使用img_fpaths(列表)中找到的图像文件来构建tfrecords。

我尝试使用tf.data.Dataset进行所有操作,以便使用并行调用和预取选项使其快速完成。

在执行全部工作之前,我正在笔记本中进行一些测试,以验证管道是否按预期工作。在我看来,它看起来像是:读取图像文件->解码图像内容->将图像内容转换为二进制->解码原始二进制数据(这最后只是为了测试,我将其替换为代码以保存到tfrecords一旦起作用)

这是我的代码:

dataset = tf.data.Dataset.from_tensor_slices(img_fpaths)
dataset = dataset.map(lambda fpath: tf.read_file(fpath))
dataset = dataset.map(lambda img_bytes: tf.image.decode_image(img_bytes))

dataset = dataset.map(lambda img: tf.as_string(tf.cast(img, dtype=tf.float32)))

# the following linke is for the test only, once this works I know how to save it as tfrecords
dataset = dataset.map(lambda img_bytes: tf.decode_raw(img_bytes, out_type=tf.float32))

此后,我运行以下代码,如果我删除了上一个dataset.map操作,则不会引发任何错误。

pair = dataset.make_one_shot_iterator().get_next()
with tf.Session() as sess:
    var = sess.run(pair)

但是由于未将其删除,因此会引发以下错误:

InvalidArgumentError            Traceback (most recent call last)
~/anaconda3/envs/vision/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1321     try:
-> 1322       return fn(*args)
   1323     except errors.OpError as e:

~/anaconda3/envs/vision/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1306       return self._call_tf_sessionrun(
-> 1307           options, feed_dict, fetch_list, target_list, run_metadata)
   1308 

~/anaconda3/envs/vision/lib/python3.5/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1408           self._session, options, feed_dict, fetch_list, target_list,
-> 1409           run_metadata)
   1410     else:

InvalidArgumentError: DecodeRaw requires input strings to all be the same size, but element 471 has size 10 != 9
 [[Node: DecodeRaw = DecodeRaw[little_endian=true, out_type=DT_FLOAT](arg0)]]
 [[Node: IteratorGetNext_23 = IteratorGetNext[output_shapes=[<unknown>, []], output_types=[DT_FLOAT, DT_STRING], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator_23)]]

我现在可以使用.tostring()方法将numpy数组转换为二进制,所以我想知道TensorFlow中是否有替代品

0 个答案:

没有答案