将batchdataset与keras vgg16 precrocess_input连接

发布于 2025-01-27 02:42:15 字数 2600 浏览 3 评论 0 原文

我正在使用 tf.keras.preprocessing.image_dataset_from_directory 获取 batchdataset ,其中数据集有10个类。

我正在尝试将此 batchdataset 与keras vgg16 docs )网络。从文档中:

注意:每个KERAS应用程序都期望特定的输入预处理。对于VGG16,请致电 tf.keras.applications.vgg16.preprocess_input 在将它们传递给模型之前。

但是,我正在努力获得此 preprocess_input batchdataset一起工作您能帮我弄清楚如何连接这两个点吗?

请参阅以下代码:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(train_data_dir, image_size=(224, 224))
train_ds = tf.keras.applications.vgg16.preprocess_input(train_ds)

这将抛出 typeerror:'batchdataset'对象无法订阅

Traceback (most recent call last):
  ...
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/vgg16.py", line 232, in preprocess_input
    return imagenet_utils.preprocess_input(
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/imagenet_utils.py", line 117, in preprocess_input
    return _preprocess_symbolic_input(
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/imagenet_utils.py", line 278, in _preprocess_symbolic_input
    x = x[..., ::-1]
TypeError: 'BatchDataset' object is not subscriptable

来自 typeError:'datasetV1adapter'对象不可订阅(来自 batchdataset试图将python字典作为表格)提示是使用:

train_ds = tf.keras.applications.vgg16.preprocess_input(
    list(train_ds.as_numpy_iterator())
)

但是,这也失败了:这

Traceback (most recent call last):
  ...
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/vgg16.py", line 232, in preprocess_input
    return imagenet_utils.preprocess_input(
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/imagenet_utils.py", line 117, in preprocess_input
    return _preprocess_symbolic_input(
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/imagenet_utils.py", line 278, in _preprocess_symbolic_input
    x = x[..., ::-1]
TypeError: list indices must be integers or slices, not tuple

一切都失败了:这都是使用 python = 3.10 == 3.10 == 3.10 .3 带有 tensorflow == 2.8.0

我该如何工作?先感谢您。

I am using tf.keras.preprocessing.image_dataset_from_directory to get a BatchDataset, where the dataset has 10 classes.

I am trying to integrate this BatchDataset with a Keras VGG16 (docs) network. From the docs:

Note: each Keras Application expects a specific kind of input preprocessing. For VGG16, call tf.keras.applications.vgg16.preprocess_input on your inputs before passing them to the model.

However, I am struggling to get this preprocess_input working with a BatchDataset. Can you please help me figure out how to connect these two dots?

Please see the below code:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(train_data_dir, image_size=(224, 224))
train_ds = tf.keras.applications.vgg16.preprocess_input(train_ds)

This will throw TypeError: 'BatchDataset' object is not subscriptable:

Traceback (most recent call last):
  ...
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/vgg16.py", line 232, in preprocess_input
    return imagenet_utils.preprocess_input(
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/imagenet_utils.py", line 117, in preprocess_input
    return _preprocess_symbolic_input(
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/imagenet_utils.py", line 278, in _preprocess_symbolic_input
    x = x[..., ::-1]
TypeError: 'BatchDataset' object is not subscriptable

From TypeError: 'DatasetV1Adapter' object is not subscriptable (from BatchDataset not subscriptable when trying to format Python dictionary as table) the suggestion was to use:

train_ds = tf.keras.applications.vgg16.preprocess_input(
    list(train_ds.as_numpy_iterator())
)

However, this also fails:

Traceback (most recent call last):
  ...
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/vgg16.py", line 232, in preprocess_input
    return imagenet_utils.preprocess_input(
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/imagenet_utils.py", line 117, in preprocess_input
    return _preprocess_symbolic_input(
  File "/path/to/venv/lib/python3.10/site-packages/keras/applications/imagenet_utils.py", line 278, in _preprocess_symbolic_input
    x = x[..., ::-1]
TypeError: list indices must be integers or slices, not tuple

This is all using Python==3.10.3 with tensorflow==2.8.0.

How can I get this working? Thank you in advance.

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

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

发布评论

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

评论(1

晨曦÷微暖 2025-02-03 02:42:15

好吧,我弄清楚了。我需要传递 tf.tensor ,而不是 tf.data.dataset 。可以通过迭代数据集来获得张量

这可以通过几种方式完成:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(...)

# Option 1
batch_images = next(iter(train_ds))[0]
preprocessed_images = tf.keras.applications.vgg16.preprocess_input(batch_images)

# Option 2:
for batch_images, batch_labels in train_ds:
    preprocessed_images = tf.keras.applications.vgg16.preprocess_input(batch_images)

如果将选项2转换为生成器,则可以直接传递到下游 model.fit 中。干杯!

Okay I figured it out. I needed to pass a tf.Tensor, not a tf.data.Dataset. One can get a Tensor out by iterating over the Dataset.

This can be done in a few ways:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(...)

# Option 1
batch_images = next(iter(train_ds))[0]
preprocessed_images = tf.keras.applications.vgg16.preprocess_input(batch_images)

# Option 2:
for batch_images, batch_labels in train_ds:
    preprocessed_images = tf.keras.applications.vgg16.preprocess_input(batch_images)

If you convert option 2 into a generator, it can be directly passed into the downstream model.fit. Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文