gpt4 book ai didi

machine-learning - 如何创建包含多个与 CIFAR10 格式相同的图像的数据集?

转载 作者:行者123 更新时间:2023-11-30 09:36:48 26 4
gpt4 key购买 nike

我有 1750*1750 的图像,我想对它们进行标记并将它们放入与 CIFAR10 格式相同的文件中。我之前看过一个类似的答案,给出了答案:

label = [3]
im = Image.open(img)
im = (np.array(im))
print(im)

r = im[:,:,0].flatten()
g = im[:,:,1].flatten()
b = im[:,:,2].flatten()
array = np.array(list(label) + list(r) + list(g) + list(b), np.uint8)

array.tofile("info.bin")

但它不包括如何在单个文件中添加多个图像。我查看了 CIFAR10 并尝试以相同的方式附加数组,但我得到的只是以下错误:

E tensorflow/core/client/tensor_c_api.cc:485] Read less bytes than requested

请注意,我使用 Tensorflow 进行计算,并且我已经能够将问题与数据隔离。

最佳答案

CIFAR-10 二进制格式将每个示例表示为具有以下格式的固定长度记录:

  • 1 字节标签。
  • 图像的红色 channel 每像素 1 个字节。
  • 图像的绿色 channel 每像素 1 个字节。
  • 图像的蓝色 channel 每像素 1 个字节。

假设您有一个名为 images 的图像文件名列表,以及与其标签相对应的名为 labels 的整数(小于 256)列表,以下代码将编写包含这些 CIFAR-10 格式图像的单个文件:

with open(output_filename, "wb") as f:
for label, img in zip(labels, images):
label = np.array(label, dtype=np.uint8)
f.write(label.tostring()) # Write label.

im = np.array(Image.open(img), dtype=np.uint8)
f.write(im[:, :, 0].tostring()) # Write red channel.
f.write(im[:, :, 1].tostring()) # Write green channel.
f.write(im[:, :, 2].tostring()) # Write blue channel.

关于machine-learning - 如何创建包含多个与 CIFAR10 格式相同的图像的数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38880654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com