改变TensorsPec TensorFlow的形状

发布于 2025-01-24 07:46:31 字数 2218 浏览 0 评论 0原文

我目前正在尝试通过转移学习实现VGG16模型,并且在尝试评估该模型时遇到了一个问题。

这就是我所说的:

initial_epochs = 10
loss0, accuracy0 = model.evaluate(validation_dataset)

这就是验证_dataset的样子:

<PrefetchDataset element_spec=(TensorSpec(shape=(None, 3, 96, 96), dtype=tf.uint8, name=None), TensorSpec(shape=(None, 4), dtype=tf.int32, name=None))>

这是我的错误消息:

ValueError                                Traceback (most recent call last)

<ipython-input-29-c7fea149280c> in <module>()
      1 initial_epochs = 10
----> 2 loss0, accuracy0 = model.evaluate(validation_dataset)

1 frames

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1525, in test_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1514, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1507, in run_step  **
        outputs = model.test_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1471, in test_step
        y_pred = self(x, training=False)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 96, 96, 3), found shape=(None, 3, 96, 96)

我尝试将模型输入形状放置为(3,96,96)而不是(96,96,3),但这不是工作... 谁能帮我吗?

I am currently trying to implement a VGG16 model via transfer learning and I'm coming across a problem when trying to evaluate the model.

this is how I call it:

initial_epochs = 10
loss0, accuracy0 = model.evaluate(validation_dataset)

and this is what validation_dataset looks like:

<PrefetchDataset element_spec=(TensorSpec(shape=(None, 3, 96, 96), dtype=tf.uint8, name=None), TensorSpec(shape=(None, 4), dtype=tf.int32, name=None))>

And this is my error message:

ValueError                                Traceback (most recent call last)

<ipython-input-29-c7fea149280c> in <module>()
      1 initial_epochs = 10
----> 2 loss0, accuracy0 = model.evaluate(validation_dataset)

1 frames

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1525, in test_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1514, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1507, in run_step  **
        outputs = model.test_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1471, in test_step
        y_pred = self(x, training=False)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 96, 96, 3), found shape=(None, 3, 96, 96)

I tried putting the model input shape as (3, 96, 96) rather than (96, 96, 3) but that doesn't work...
Can anyone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2025-01-31 07:46:31

尝试使用validation_dataset.maptf.transpose

import tensorflow as tf

images = tf.random.normal((10, 3, 96, 96))
labels = tf.random.uniform((10, 4),  maxval=4, dtype=tf.int32)
validation_dataset = tf.data.Dataset.from_tensor_slices((images, labels)).batch(2)
print(validation_dataset)
validation_dataset = validation_dataset.map(lambda x, y: (tf.transpose(x, perm=[0, 3, 2, 1]), y))
print(validation_dataset)
<BatchDataset element_spec=(TensorSpec(shape=(None, 3, 96, 96), dtype=tf.float32, name=None), TensorSpec(shape=(None, 4), dtype=tf.int32, name=None))>
<MapDataset element_spec=(TensorSpec(shape=(None, 96, 96, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None, 4), dtype=tf.int32, name=None))>

Try using validation_dataset.map and tf.transpose:

import tensorflow as tf

images = tf.random.normal((10, 3, 96, 96))
labels = tf.random.uniform((10, 4),  maxval=4, dtype=tf.int32)
validation_dataset = tf.data.Dataset.from_tensor_slices((images, labels)).batch(2)
print(validation_dataset)
validation_dataset = validation_dataset.map(lambda x, y: (tf.transpose(x, perm=[0, 3, 2, 1]), y))
print(validation_dataset)
<BatchDataset element_spec=(TensorSpec(shape=(None, 3, 96, 96), dtype=tf.float32, name=None), TensorSpec(shape=(None, 4), dtype=tf.int32, name=None))>
<MapDataset element_spec=(TensorSpec(shape=(None, 96, 96, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None, 4), dtype=tf.int32, name=None))>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文