我有图像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进行计算,并且能够将问题与数据隔离开来。
答案 0 :(得分:2)
CIFAR-10二进制格式将每个示例表示为具有以下格式的固定长度记录:
假设您有一个名为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.