我正在使用TensorFlow 2.0,我有一个批处理的数据集,其中包含968个图像和每个图像的标签(4个元素数组):
dataSetSize = allDataSet.reduce(0, lambda x, _: x + 1).numpy()
allDataSet = allDataSet.shuffle(dataSetSize)
allDataSet = allDataSet.map(processPath, num_parallel_calls=tf.data.experimental.AUTOTUNE)
allDataSet = allDataSet.batch(10)
predictions = loadedModel.predict(allDataSet)
onlyImages = # how to create this?
onlyLabels = # how to create this?
# the 'map' function in my dataset returns a batch of images and their corresponding labels
for idx, (imageBatch, labelBatch) in enumerate(allDataSet) :
# how to concatenate batches together?
onlyImages = # ?
onlyLabels = # ?
我需要将此数据集分成两个numpy数组。第一个数组应仅包含968个图像(形状:(968,299,299,3)),第二个数组应包含968个标签(形状:(968,4))。我怎样才能做到这一点?
我在这里检查了类似的问题,但这些示例似乎正在使用Tensorflow 1.x,并且由不同的输入类型组成?
数据集和类型的大小:
dataset size: 968
<DatasetV1Adapter shapes: ((None, 299, 299, 3), (None, 4)), types: (tf.float32, tf.float32)
如果我很好地理解了您的问题,那么现在您需要做的就是在numpy
遍历数据集时将其连接到一个数组。请注意,在迭代过程中,如果应用.numpy()
操作,则会自动从转换tf.tensor
为np.array
。
因此,可以使用以下选项:
根据文档,
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.concatenate((a, b), axis=0)
输出为:
array([[1, 2],
[3, 4],
[5, 6]])
因此,在您的代码中,定义要连接的初始空numpy数组axis=0
(使用imageBatch和labelBatch)。
np.vstack
(引擎盖下np.concatenate
使用np.vstack
)提供相同的结果。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句