unet keras 图像分割中的序列模型错误

问题描述 投票:0回答:1

我目前正在学习 U net,我正在尝试运行以下示例代码: https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/vision/ ipynb/oxford_pets_image_segmentation.ipynb#scrollTo=ge5jm8VSD_bs 但是,我在“augmented_train_ds”中遇到错误,并且不知道如何解决此问题:

augmented_train_ds = (
    train_ds.shuffle(BATCH_SIZE * 2)
    .map(augment_fn, num_parallel_calls=AUTOTUNE)
    .batch(BATCH_SIZE)
    .map(unpackage_inputs)
    .prefetch(buffer_size=tf.data.AUTOTUNE)
)

错误如下所示:

WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor. Received: inputs={'images': <tf.Tensor 'args_0:0' shape=(None, None, 3) dtype=float32>, 'segmentation_masks': <tf.Tensor 'args_1:0' shape=(None, None, 1) dtype=uint8>}. Consider rewriting this model with the Functional API.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-e2034c8f5b68> in <cell line: 2>()
      1 augmented_train_ds = (
----> 2     train_ds.shuffle(BATCH_SIZE * 2)
      3     .map(augment_fn, num_parallel_calls=AUTOTUNE)
      4     .batch(BATCH_SIZE)
      5     .map(unpackage_inputs)

25 frames
/usr/local/lib/python3.10/dist-packages/keras_cv/layers/preprocessing/random_choice.py in tf___augment(self, inputs, *args, **kwargs)
     12                 try:
     13                     do_return = True
---> 14                     retval_ = ag__.converted_call(ag__.ld(tf).switch_case, (), dict(branch_index=ag__.ld(selected_op), branch_fns=ag__.ld(branch_fns), default=ag__.autograph_artifact(lambda : ag__.ld(inputs))), fscope)
     15                 except:
     16                     do_return = False

TypeError: Exception encountered when calling layer 'rand_augment' (type RandAugment).

in user code:

    File "/usr/local/lib/python3.10/dist-packages/keras_cv/layers/preprocessing/base_image_augmentation_layer.py", line 419, in call  *
        outputs = self._format_output(self._augment(inputs), metadata)
    File "/usr/local/lib/python3.10/dist-packages/keras_cv/layers/preprocessing/rand_augment.py", line 127, in _augment  *
        result = super()._augment(sample)
    File "/usr/local/lib/python3.10/dist-packages/keras_cv/layers/preprocessing/random_augmentation_pipeline.py", line 103, in _augment  *
        result = tf.cond(
    File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/tmp/__autograph_generated_fileuxkhl0zq.py", line 46, in tf__call
        ag__.if_stmt(ag__.ld(images).shape.rank == 3, if_body_1, else_body_1, get_state_1, set_state_1, ('outputs',), 1)
    File "/tmp/__autograph_generated_fileuxkhl0zq.py", line 24, in if_body_1
        outputs = ag__.converted_call(ag__.ld(self)._format_output, (ag__.converted_call(ag__.ld(self)._augment, (ag__.ld(inputs),), None, fscope), ag__.ld(metadata)), None, fscope)
    File "/tmp/__autograph_generated_filef0m6xu7q.py", line 14, in tf___augment
        retval_ = ag__.converted_call(ag__.ld(tf).switch_case, (), dict(branch_index=ag__.ld(selected_op), branch_fns=ag__.ld(branch_fns), default=ag__.autograph_artifact(lambda : ag__.ld(inputs))), fscope)

    TypeError: Exception encountered when calling layer 'random_choice' (type RandomChoice).
    
    in user code:
    
        File "/usr/local/lib/python3.10/dist-packages/keras_cv/layers/preprocessing/base_image_augmentation_layer.py", line 419, in call  *
            outputs = self._format_output(self._augment(inputs), metadata)
        File "/usr/local/lib/python3.10/dist-packages/keras_cv/layers/preprocessing/random_choice.py", line 110, in _augment  *
            default=lambda: inputs,
    
        TypeError: branches[0] and branches[1] arguments to tf.switch_case must have the same number, type, and overall structure of return values.
        
        branches[0] output: {'images': <tf.Tensor 'sequential/rand_augment/cond/random_choice/switch_case/indexed_case/Identity:0' shape=(160, 160, 3) dtype=float32>, 'segmentation_masks': <tf.Tensor 'sequential/rand_augment/cond/random_choice/switch_case/indexed_case/Identity_1:0' shape=(160, 160, 1) dtype=float32>}
        branches[1] output: {'images': <tf.Tensor 'sequential/rand_augment/cond/random_choice/switch_case/indexed_case/Identity:0' shape=(160, 160, 3) dtype=float32>, 'segmentation_masks': <tf.Tensor 'sequential/rand_augment/cond/random_choice/switch_case/indexed_case/Identity_1:0' shape=(160, 160, 1) dtype=int64>}
        
        Error details:
        Tensor("sequential/rand_augment/cond/random_choice/switch_case/indexed_case/Identity_1:0", shape=(160, 160, 1), dtype=float32) and Tensor("sequential/rand_augment/cond/random_choice/switch_case/indexed_case/Identity_1:0", shape=(160, 160, 1), dtype=int64) have different types
    
    
    Call arguments received by layer 'random_choice' (type RandomChoice):
      • inputs={'images': 'tf.Tensor(shape=(160, 160, 3), dtype=float32)', 'segmentation_masks': 'tf.Tensor(shape=(160, 160, 1), dtype=int64)'}


Call arguments received by layer 'rand_augment' (type RandAugment):
  • inputs={'images': 'tf.Tensor(shape=(160, 160, 3), dtype=float32)', 'segmentation_masks': 'tf.Tensor(shape=(160, 160, 1), dtype=int64)'}

请帮我举个例子和解释。

keras image-segmentation unet-neural-network
1个回答
0
投票

好吧,据我从 Torchvision 的文档中看到,RandAugment 依赖于 uint8 的图像,而此时的图像是 dfloat32。因此,教程中给出的代码可能永远无法工作。

我刚刚注释掉了相应的行:

#keras_cv.layers.RandAugment(
#             value_range=(0, 1),
#             geometric=False,
#         ),

本教程在没有额外增强的情况下仍然有效。

© www.soinside.com 2019 - 2024. All rights reserved.