我的图像分类任务中有以下部分代码。对于这个任务,我必须在文件夹中的图像上训练模型,并测试模型并预测准确性
import tensorflow
import keras
from tensorflow.keras import preprocessing
data1 = preprocessing.image_dataset_from_directory("folder",label_mode="categorical",seed=0, image_size=(100,120))
data2 = preprocessing.image_dataset_from_directory("folder",label_mode="categorical",seed=0, image_size=(100,120))
type(data1)
type(data2)
运行此代码后,结果将存储在data1和data2
中。data1和data2的类型是类tensorflow.python.data.ops.dataset_ops.BatchDataset
我想把data2附加到data1,这样我就可以使用结果数据来训练模型。
我之前尝试过连接
'data1.concatenate (data2)
'它在测试中失败并抛出错误categorical_crossentropy target.shape.assert_is_compatible_with(output.shape)ValueError:形状(None, 1)和(None, 2)不兼容
我也试过np.append()在训练期间失败并抛出以下错误
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type BatchDataset)
但是我必须使用追加来链接数据例如data1
[(array([[[[165., 166., 170.],
[165., 166., 170.],
[165., 166., 170.],
[158., 157., 155.],
[158., 157., 155.],
[157., 156., 154.]],
data2
[(array([[[[161., 165., 168.],
[161., 165., 168.],
[161., 165., 168.],
[157., 160., 149.],
[157., 160., 149.],
[157., 160., 149.]],
附加的数据应该是
[(array([[[[165., 166., 170.],
[165., 166., 170.],
[165., 166., 170.],
[158., 157., 155.],
[158., 157., 155.],
[157., 156., 154.]]
[161., 165., 168.],
[161., 165., 168.],
[161., 165., 168.],
[157., 160., 149.],
[157., 160., 149.],
[157., 160., 149.]]
我按照这个https://stackoverflow.com/questions/73934511/how-to-concatenate-tensorflow-python-data-ops-dataset-ops-batchdataset进行连接
我已经执行了下面的代码
import tensorflow as tf
from tensorflow.keras import preprocessing
data1 = preprocessing.image_dataset_from_directory("/content/",label_mode="categorical",seed=0, image_size=(100,120))
output:Found 809 files belonging to 4 classes.
data2 = preprocessing.image_dataset_from_directory("/content/Pistachio_Image_Dataset",label_mode="categorical",seed=0, image_size=(100,120))
output:Found 2148 files belonging to 3 classes.
combined_dataset = data1.concatenate(data2)
代码运行良好,不产生任何错误。谢谢你。