简体   繁体   English

无法将 tf.keras.preprocessing.image_dataset_from_directory 转换为 np.array

[英]Cannot convert tf.keras.preprocessing.image_dataset_from_directory to np.array

I am trying to create a image classification model using CNN.我正在尝试使用 CNN 创建图像分类 model。 For that I am reading the data using the tf.keras.preprocessing.image_dataset_from_directory function.为此,我正在使用tf.keras.preprocessing.image_dataset_from_directory function 读取数据。

This is the code:这是代码:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(data_dir_train,seed=123,validation_split = 0.2,subset = 'training',image_size=(img_height, img_width),batch_size=batch_size)

Then I am trying to convert the dataset in np.array object.然后我试图在 np.array object 中转换数据集。 My code is我的代码是

x_train = np.array(train_ds)

But when I print x_train , I am getting但是当我打印x_train时,我得到了

array(<BatchDataset shapes: ((None, 180, 180, 3), (None,)), types: (tf.float32, tf.int32)>, dtype=object)

The object train_ds is of shape (2000,180,180,3) . object train_ds的形状为(2000,180,180,3) I am not sure what is wrong in my code.我不确定我的代码有什么问题。

When using image_dataset_from_directory :使用image_dataset_from_directory时:

... image_dataset_from_directory(main_directory, labels='inferred') will return a tf.data.Dataset that yields batches of images from the subdirectories... ... image_dataset_from_directory(main_directory, labels='inferred') 将返回一个tf.data.Dataset ,它会从子目录中生成批量图像...

One option to get the data you want, is to use take to create a Dataset with at most count elements from this dataset.获取所需数据的一种方法是使用take创建一个数据集,其中最多包含该数据集中的count 个元素。

import tensorflow as tf
import numpy as np

img = np.empty((6000,180,180,3), dtype=np.float32)
label = np.empty((6000,), dtype=np.int32)

train_ds = tf.data.Dataset.from_tensor_slices((img,label)).batch(2000)
print(train_ds) # <BatchDataset shapes: ((None, 180, 180, 3), (None,)), types: (tf.float32, tf.int32)>

for batch in train_ds.take(1):
    img_batch, label_batch = batch
    print(img_batch.shape) # (2000, 180, 180, 3)
    print(label_batch.shape) # (2000,)

Depending on how you're structuring your code, you may not even need to convert to a numpy.array , since tf.keras.Model fit accepts tf.data datasets.根据您构建代码的方式,您甚至可能不需要转换为numpy.array ,因为tf.data fit数据集。

model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 tf.keras.preprocessing.image_dataset_from_directory() 探索和修改创建的数据集? - How can I explore and modify the created dataset from tf.keras.preprocessing.image_dataset_from_directory()? 如何使用 tf.keras.preprocessing.image_dataset_from_directory() 创建具有特定形状的数据集? - How do I use tf.keras.preprocessing.image_dataset_from_directory() to create a dataset with a certain shape? 来自 tf.keras.preprocessing.image_dataset_from_directory 的 x_test 和 y_test - x_test and y_test from tf.keras.preprocessing.image_dataset_from_directory ValueError:使用 tf.keras.preprocessing.image_dataset_from_directory 时要解压的值太多(预期为 2) - ValueError: too many values to unpack (expected 2) when using tf.keras.preprocessing.image_dataset_from_directory tf.keras.preprocessing.image_dataset_from_directory 值错误:找不到图像 - tf.keras.preprocessing.image_dataset_from_directory Value Error: No images found 使用 tf.keras.preprocessing.image_dataset_from_directory 的 tf.data.Dataset 训练模型是非常慢的 keras - train model using tf.data.Dataset of tf.keras.preprocessing.image_dataset_from_directory is very slow keras 设置一次后更改 tf.keras.preprocessing.image_dataset_from_directory 的 label_mode - Changing label_mode of tf.keras.preprocessing.image_dataset_from_directory after setting it once 如何使用 tf.keras.preprocessing.image_dataset_from_directory 获取类的数量? - how to obtain the number of classes using tf.keras.preprocessing.image_dataset_from_directory? tf.keras.preprocessing.image_dataset_from_directory 如何将 output 显示到控制台 - How tf.keras.preprocessing.image_dataset_from_directory display output to console 当我们在 tf.keras.preprocessing.image_dataset_from_directory 对象上使用 .next() 或 .take() 时,我们是否会丢失数据? - Are we loosing data when we use .next() or .take() on tf.keras.preprocessing.image_dataset_from_directory object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM