在C#中读取emnist数据库

时间:2017-10-20 08:43:52

标签: c# mnist

我有一个需要帮助的问题。我需要阅读EMNIST数据库,但无法解决如何做到这一点。该代码适用于MNIST数据库,但不适用于EMNIST数据库,即使它们应该采用相同的二进制格式。任何人都可以帮助我吗?

读取图像数据的代码示例

Object

1 个答案:

答案 0 :(得分:1)

我可以使用以下代码读取MNIST和EMNIST:

string imagesPath = @"C:\Path\To\Images";
string labelsPath = @"C:\Path\To\Labels";
using (BinaryReader brImages = new BinaryReader(new FileStream(imagesPath, FileMode.Open)), brLabels = new BinaryReader(new FileStream(labelsPath, FileMode.Open)))
{
    int magic1 = brImages.ReadInt32Endian();
    if (magic1 != 2051)
        throw new Exception($"Invalid magic number {magic1}!");
    int numImages = brImages.ReadInt32Endian();
    int numRows = brImages.ReadInt32Endian();
    int numCols = brImages.ReadInt32Endian();

    Console.WriteLine($"Loading {numImages} images with {numRows} rows and {numCols} columns...");

    int magic2 = brLabels.ReadInt32Endian();
    if (magic2 != 2049)
        throw new Exception($"Invalid magic number {magic2}!");
    int numLabels = brLabels.ReadInt32Endian();
    if (numLabels != numImages)
        throw new Exception($"Number of labels ({numLabels}) does not equal number of images ({numImages})");

    byte[][] images = new byte[numImages][];
    byte[] labels = new byte[numLabels];
    int dimensions = numRows * numCols;
    for (int i = 0; i < numImages; i++)
    {
        images[i] = brImages.ReadBytes(dimensions);
        labels[i] = brLabels.ReadByte();
    }
}

我使用BinaryReader的以下扩展来纠正字节顺序:

static class BinaryReaderExtension
{
    public static int ReadInt32Endian(this BinaryReader br)
    {
        var bytes = br.ReadBytes(sizeof(Int32));
        if (BitConverter.IsLittleEndian)
            Array.Reverse(bytes);
        return BitConverter.ToInt32(bytes, 0);
    }
}