簡體   English   中英

無法使用 tf.io.decode_image 解碼批量圖像

[英]Unable to decode batch of images with tf.io.decode_image

我正在嘗試運行面部關鍵點回歸。 我成功地創建了 TFRecord 文件,其中編碼了圖像和標簽(標簽是面部 kypoints)。

然后,我開始將數據(圖像和關鍵點)加載到內存中(遵循https://gist.github.com/FirefoxMetzger/c143c340c71e85c0c23c7ced94a88c16#file-faster_fully_connected_reader-py 中的指南)。 我想首先對所有圖像進行批處理,然后按照該指南中的描述對圖像進行解碼。 但是,這不起作用。 如果我的理解是正確的,我只能在單個圖像上而不是在批處理上使用 tf.image.decode_image() 。 我的理解正確嗎? 如果是,我該如何解碼一批圖像?

先感謝您!

抄送

這是代碼:

ds = tf.data.TFRecordDataset(TFR_FILENAME)

ds = ds.repeat(EPOCHS)

ds = ds.shuffle(BUFFER_SIZE + BATCH_SIZE)

ds = ds.batch(BATCH_SIZE)

finally I tried to decode the image using tf.image.decode_image()

feature_description = {'height': tf.io.FixedLenFeature([], tf.int64),
                    'width': tf.io.FixedLenFeature([], tf.int64),
                    'depth': tf.io.FixedLenFeature([], tf.int64),
                    'kpts': tf.io.FixedLenFeature([136], tf.float32),
                    'image_raw': tf.io.FixedLenFeature([], tf.string),
                    }

for record in ds.take(1):
    record = tf.io.parse_example(record, feature_description)
    decoded_image = tf.io.decode_image(record['image_raw'], dtype=tf.float32)

這會引發以下 ValueError:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-3583be9c40ab> in <module>()
      1 for record in ds.take(1):
      2     record = tf.io.parse_example(record, feature_description)
----> 3     decoded_image = tf.io.decode_image(record['image_raw'], dtype=tf.float32)

3 frames
/tensorflow-2.0.0/python3.6/tensorflow_core/python/ops/image_ops_impl.py in decode_image(contents, channels, dtype, name, expand_animations)
   2315     # as well as JPEG images with EXIF data (start with \xff\xd8\xff\xe1).
   2316     return control_flow_ops.cond(
-> 2317         is_jpeg(contents), _jpeg, check_png, name='cond_jpeg')
   2318 
   2319 

/tensorflow-2.0.0/python3.6/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

/tensorflow-2.0.0/python3.6/tensorflow_core/python/ops/control_flow_ops.py in cond(pred, true_fn, false_fn, strict, name, fn1, fn2)
   1199   with ops.name_scope(name, "cond", [pred]):
   1200     if context.executing_eagerly():
-> 1201       if pred:
   1202         result = true_fn()
   1203       else:

/tensorflow-2.0.0/python3.6/tensorflow_core/python/framework/ops.py in __bool__(self)
    874 
    875   def __bool__(self):
--> 876     return bool(self._numpy())
    877 
    878   __nonzero__ = __bool__

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

實際上, decode_image僅適用於單個圖像。 在批處理之前,您仍然應該通過在數據集中進行解碼來獲得合理的性能。

像這樣(代碼未經測試,可能需要一些調整):

ds = tf.data.TFRecordDataset(TFR_FILENAME)

def parse_and_decode(record):
  record = tf.io.parse_example(record, feature_description)
  record['image'] = tf.io.decode_image(record['image_raw'], dtype=tf.float32)
  return record

ds = ds.map(parse_and_decode)

ds = ds.repeat(EPOCHS)

ds = ds.shuffle(BUFFER_SIZE + BATCH_SIZE)
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM