【问题标题】:How to quickly transform jpg datasets into .pkl for CNN?如何快速将 jpg 数据集转换为 .pkl 用于 CNN?
【发布时间】:2017-05-05 09:35:34
【问题描述】:

我很难找到用于这种转变的资源。我看到一些示例代码中的输入数据采用 .pkl 格式,而 mnist 数据集采用 .idx3-ubyte 格式。用于计算视觉的数据集格式也各不相同。我不熟悉任何格式,如果能对这个问题有所了解,我将不胜感激。谢谢。


更新:现在我使用下面的代码成功地以 .tfrecords 格式加载了我的图像,但由于 cnn 似乎无法读取这种格式,我仍在尝试修改 .pkl 格式的代码。但是,我的运行都失败了。

            cwd='/Users/Downloads/tflearn_train/'
            classes={'0','1'} #classify into 2 types
            writer= tf.python_io.TFRecordWriter("train.tfrecords") #file to be produced

            for index,name in enumerate(classes):
                class_path=cwd+name+'/'
                for img_name in os.listdir(class_path):
                    if (not img_name.startswith('.') and img_name != 'Thumbs.db'):
                        img_path=class_path+img_name #the path of every pic
                        img=Image.open(img_path,"r")
                        img= img.resize((224,224))
                        img_raw=img.tobytes()#transform pic into binary
                        example = tf.train.Example(features=tf.train.Features(feature={
                            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
                            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
                        }))
                        writer.write(example.SerializeToString())
            writer.close()

以上工作正常。但我试着把

        write_file = open('train.pkl', 'wb')
        cPickle.dump(example, write_file, -1)
        cPickle.dump(example.features.feature['label'].int64_list.value, write_file, -1)
        write_file.close()

循环内部和外部。到目前为止,当使用 cPickle.load 时,我未能制作一个看起来像其他 .pkl 文件的 .pkl 文件

感谢您的每一个意见。

【问题讨论】:

    标签: python image neural-network pickle conv-neural-network


    【解决方案1】:

    Pickle 存储有关 python 对象结构的信息以及数据。对于简单的张量,这可能不是必需的。

    相反,通常的方法是将矩阵数据以二进制格式转储到文件中,然后直接重新加载回内存。我相信用于 MNIST 图形数据集的“.idx3-ubyte”就是这样一个例子。

    如果你使用 python 和 numpy,你最好使用 numpy 的 .npy 格式,它使用 np.load 和 np.dump 函数稍微简化了过程:https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.load.html。

    如果您需要加载二进制数据转储,请查看https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html

    【讨论】:

    • 感谢您的意见。我已经成功地以 .tfrecords 形式加载了这些图像和标签,但我找不到任何可以加载这种格式的文件来训练 cnn 的代码。
    • 假设 tf 代表 tensorflow,我没有这方面的经验,抱歉。
    猜你喜欢
    • 2019-08-18
    • 2021-12-11
    • 1970-01-01
    • 2011-07-02
    • 2019-04-04
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多