如何创建类似于 cifar-10 的数据集
How to create dataset similar to cifar-10
我想创建一个与 cifar-10 数据集格式相同的数据集以用于 Tensorflow。它应该有图像和标签。我希望能够使用 cifar-10 代码但不同的图像和标签,以及 运行 该代码。
首先我们需要了解CIFAR10数据集的格式。如果我们参考:https://www.cs.toronto.edu/~kriz/cifar.html,具体来说,二进制版本部分,我们看到:
the first byte is the label of the first image, which
is a number in the range 0-9. The next 3072 bytes are the values of
the pixels of the image. The first 1024 bytes are the red channel
values, the next 1024 the green, and the final 1024 the blue. The
values are stored in row-major order, so the first 32 bytes are the
red channel values of the first row of the image.
直观上,我们需要以这种格式存储数据。作为基线实验,您接下来可以做的是获取与 CIFAR10 大小和 类 数量完全相同的图像,并将它们置于这种格式中。这意味着您的图像应具有 32x32x3 的大小和 10 类。如果你能成功 运行 这个,那么你可以进一步处理单通道、不同大小输入和不同 类 等因素情况。这样做意味着您必须更改代码其他部分的许多变量。你得慢慢来。
我正在制定通用模块。我的代码在 https://github.com/jkschin/svhn 中。如果您参考 svhn_flags.py 代码,您会看到那里有许多可以更改以满足您的需要的标志。我承认它现在很神秘,因为我还没有清理它以使其可读,但它确实有效。如果你愿意花一些时间粗略地看一看,你就会明白一些事情。
这可能是在 CIFAR10 上 运行 您自己的数据集的简单方法。您当然可以只复制神经网络定义并实现您自己的 reader、输入格式、批处理等,但是如果您想要它并 运行ning 快速,只需调整您的输入以适应 CIFAR10。
编辑:
一些非常非常基本的代码,希望对您有所帮助。
from PIL import Image
import numpy as np
im = Image.open('images.jpeg')
im = (np.array(im))
r = im[:,:,0].flatten()
g = im[:,:,1].flatten()
b = im[:,:,2].flatten()
label = [1]
out = np.array(list(label) + list(r) + list(g) + list(b),np.uint8)
out.tofile("out.bin")
这会将图像转换为可在 CIFAR10 中使用的字节文件。对于多张图片,只需继续连接数组,如上面的格式所述。要检查您的格式是否正确,特别是对于 Asker 的用例,您应该获得 4274273 + 1 = 546988 字节的文件大小。假设您的图片是 RGB 并且值范围为 0-255。一旦你验证了这一点,你就已经在 TensorFlow 中设置为 运行。一定要使用 TensorBoard 来可视化一张图片,只是为了保证正确性。
编辑 2:
根据提问者在评论中的问题,
if not eval_data:
filenames = [os.path.join(data_dir, 'data_batch_%d.bin' % i)
for i in xrange(1, 6)]
如果你真的想让它按原样工作,你需要研究CIFAR10代码的函数调用。在 cifar10_input 中,批次是硬编码的。所以你必须编辑这行代码以适合 bin 文件的名称。或者,将您的图片平均分配到 6 个 bin 文件中。
对于 SVHN 数据集
对于多个输入图像,您可以这样尝试:
import numpy as np
import scipy.io
mat = scipy.io.loadmat('train_32x32.mat')
data = mat['X']
label = mat['y']
R_data = data[:,:,0,:]
G_data = data[:,:,1,:]
B_data = data[:,:,2,:]
R_data = np.transpose(R_data, (2,0,1))
G_data = np.transpose(G_data, (2,0,1))
B_data = np.transpose(B_data, (2,0,1))
R_data = np.reshape(R_data,(73257,32*32))
G_data = np.reshape(G_data,(73257,32*32))
B_data = np.reshape(B_data,(73257,32*32))
outdata = np.concatenate((label,R_data,G_data,B_data), axis = 1)
step = 10000
for i in range(1,6):
temp = outdata[i*step:(i+1)*step,:]
temp.tofile('SVHN_train_data_batch%d.bin' % i)
print('save data %d' % i)
我没有找到任何答案来做我想做的事我制定了自己的解决方案。它可以在我的 github 上找到:https://github.com/jdeepee/machine_learning/tree/master
此脚本会将大量图像转换为训练和测试数据,其中数组的形状与 cifar10 数据集相同。
代码已注释,因此应该很容易理解。我应该注意到它遍历了一个包含多个包含图像的文件夹的主目录。
我想创建一个与 cifar-10 数据集格式相同的数据集以用于 Tensorflow。它应该有图像和标签。我希望能够使用 cifar-10 代码但不同的图像和标签,以及 运行 该代码。
首先我们需要了解CIFAR10数据集的格式。如果我们参考:https://www.cs.toronto.edu/~kriz/cifar.html,具体来说,二进制版本部分,我们看到:
the first byte is the label of the first image, which is a number in the range 0-9. The next 3072 bytes are the values of the pixels of the image. The first 1024 bytes are the red channel values, the next 1024 the green, and the final 1024 the blue. The values are stored in row-major order, so the first 32 bytes are the red channel values of the first row of the image.
直观上,我们需要以这种格式存储数据。作为基线实验,您接下来可以做的是获取与 CIFAR10 大小和 类 数量完全相同的图像,并将它们置于这种格式中。这意味着您的图像应具有 32x32x3 的大小和 10 类。如果你能成功 运行 这个,那么你可以进一步处理单通道、不同大小输入和不同 类 等因素情况。这样做意味着您必须更改代码其他部分的许多变量。你得慢慢来。
我正在制定通用模块。我的代码在 https://github.com/jkschin/svhn 中。如果您参考 svhn_flags.py 代码,您会看到那里有许多可以更改以满足您的需要的标志。我承认它现在很神秘,因为我还没有清理它以使其可读,但它确实有效。如果你愿意花一些时间粗略地看一看,你就会明白一些事情。
这可能是在 CIFAR10 上 运行 您自己的数据集的简单方法。您当然可以只复制神经网络定义并实现您自己的 reader、输入格式、批处理等,但是如果您想要它并 运行ning 快速,只需调整您的输入以适应 CIFAR10。
编辑:
一些非常非常基本的代码,希望对您有所帮助。
from PIL import Image
import numpy as np
im = Image.open('images.jpeg')
im = (np.array(im))
r = im[:,:,0].flatten()
g = im[:,:,1].flatten()
b = im[:,:,2].flatten()
label = [1]
out = np.array(list(label) + list(r) + list(g) + list(b),np.uint8)
out.tofile("out.bin")
这会将图像转换为可在 CIFAR10 中使用的字节文件。对于多张图片,只需继续连接数组,如上面的格式所述。要检查您的格式是否正确,特别是对于 Asker 的用例,您应该获得 4274273 + 1 = 546988 字节的文件大小。假设您的图片是 RGB 并且值范围为 0-255。一旦你验证了这一点,你就已经在 TensorFlow 中设置为 运行。一定要使用 TensorBoard 来可视化一张图片,只是为了保证正确性。
编辑 2:
根据提问者在评论中的问题,
if not eval_data:
filenames = [os.path.join(data_dir, 'data_batch_%d.bin' % i)
for i in xrange(1, 6)]
如果你真的想让它按原样工作,你需要研究CIFAR10代码的函数调用。在 cifar10_input 中,批次是硬编码的。所以你必须编辑这行代码以适合 bin 文件的名称。或者,将您的图片平均分配到 6 个 bin 文件中。
对于 SVHN 数据集 对于多个输入图像,您可以这样尝试:
import numpy as np
import scipy.io
mat = scipy.io.loadmat('train_32x32.mat')
data = mat['X']
label = mat['y']
R_data = data[:,:,0,:]
G_data = data[:,:,1,:]
B_data = data[:,:,2,:]
R_data = np.transpose(R_data, (2,0,1))
G_data = np.transpose(G_data, (2,0,1))
B_data = np.transpose(B_data, (2,0,1))
R_data = np.reshape(R_data,(73257,32*32))
G_data = np.reshape(G_data,(73257,32*32))
B_data = np.reshape(B_data,(73257,32*32))
outdata = np.concatenate((label,R_data,G_data,B_data), axis = 1)
step = 10000
for i in range(1,6):
temp = outdata[i*step:(i+1)*step,:]
temp.tofile('SVHN_train_data_batch%d.bin' % i)
print('save data %d' % i)
我没有找到任何答案来做我想做的事我制定了自己的解决方案。它可以在我的 github 上找到:https://github.com/jdeepee/machine_learning/tree/master
此脚本会将大量图像转换为训练和测试数据,其中数组的形状与 cifar10 数据集相同。
代码已注释,因此应该很容易理解。我应该注意到它遍历了一个包含多个包含图像的文件夹的主目录。