Ejemplo n.º 1
0
 static void NPZInputStreamPeekInvalidFilename()
 {
     using (var stream = new NPZInputStream(Test.AssetPath("test.npz")))
     {
         var header = stream.Peek("not_there.npy");
     }
 }
Ejemplo n.º 2
0
 static void NPZInputStreamReadInvalidFilename()
 {
     using (var stream = new NPZInputStream(Test.AssetPath("test.npz")))
     {
         var tensor = stream.ReadUInt8("not_there.npy");
     }
 }
Ejemplo n.º 3
0
        static void TestRead(bool compressed, ref int result)
        {
            UInt8Tensor   expectedColor = Test.Tensor <UInt8Tensor, byte, UInt8Buffer>(new Shape(new uint[] { 5, 5, 3 }));
            Float32Tensor expectedDepth = Test.Tensor <Float32Tensor, float, Float32Buffer>(new Shape(new uint[] { 5, 5 }));

            string         filename = compressed ? "test_compressed.npz" : "test.npz";
            NPZInputStream stream   = new NPZInputStream(Test.AssetPath(filename));

            UInt8Tensor   actualColor = stream.ReadUInt8("color.npy");
            Float32Tensor actualDepth = stream.ReadFloat32("depth.npy");

            string tag = "c#_npz_read";

            if (compressed)
            {
                tag += "_compressed";
            }

            Test.AssertEqual <byte, UInt8Buffer>(expectedColor, actualColor, ref result, tag + " color");
            Test.AssertEqual <float, Float32Buffer>(expectedDepth, actualDepth, ref result, tag + " depth");
        }
Ejemplo n.º 4
0
        static void TestPeek(bool compressed, ref int result)
        {
            HeaderInfo expectedColor = new HeaderInfo(DataType.UINT8, Endian.NATIVE, false, new Shape(new uint[] { 5, 5, 3 }));
            HeaderInfo expectedDepth = new HeaderInfo(DataType.FLOAT32, Endian.LITTLE, false, new Shape(new uint[] { 5, 5 }));

            string         filename = compressed ? "test_compressed.npz" : "test.npz";
            NPZInputStream stream   = new NPZInputStream(Test.AssetPath(filename));

            HeaderInfo actualColor = stream.Peek("color.npy");
            HeaderInfo actualDepth = stream.Peek("depth.npy");

            string tag = "c#_npz_read";

            if (compressed)
            {
                tag += "_compressed";
            }

            Test.AssertEqual(false, stream.Contains("not_there.npy"), ref result, tag + " contains not_there");
            Test.AssertEqual(true, stream.Contains("color.npy"), ref result, tag + " contains color");
            Test.AssertEqual(true, stream.Contains("depth.npy"), ref result, tag + " contains depth");
            Test.AssertEqual(expectedColor, actualColor, ref result, tag + " color");
            Test.AssertEqual(expectedDepth, actualDepth, ref result, tag + " depth");
        }
Ejemplo n.º 5
0
    public static void Main(string[] args)
    {
        // create a tensor object
        Shape       shape = new Shape(new uint[] { 32, 32, 3 });
        UInt8Tensor color = new UInt8Tensor(shape);

        // fill it with some data.
        for (uint row = 0; row < color.Shape[0]; ++row)
        {
            for (uint col = 0; col < color.Shape[1]; ++col)
            {
                color[row, col, 0] = (byte)(row << 3);
                color[row, col, 1] = (byte)(col << 3);
                color[row, col, 2] = 128;
            }
        }

        // save it to disk as an NPY file
        color.Save("color.npy");

        // we can manually set the endianness to use
        color.Save("color.npy", Endian.BIG);

        // we can peek at the header of a file
        HeaderInfo header = NumpyIO.NumpyIO.Peek("color.npy");

        // we can load it using the path constructor
        color = new UInt8Tensor("color.npy");

        // let's create a second tensor as well
        shape = new Shape(new uint[] { 32, 32 });
        Float32Tensor gray = new Float32Tensor(shape);

        for (uint row = 0; row < gray.Shape[0]; ++row)
        {
            for (uint col = 0; col < gray.Shape[1]; ++col)
            {
                gray[row, col] = 0.21f * color[row, col, 0] +
                                 0.72f * color[row, col, 1] +
                                 0.07f * color[row, col, 2];
            }
        }

        // we can write them to an NPZ file
        NPZOutputStream output = new NPZOutputStream("test.npz");

        output.Write("color.npy", color);
        output.Write("gray.npy", gray);
        output.Close();

        // and we can read them back out again
        NPZInputStream input = new NPZInputStream("test.npz");

        // we can check of an archive contains a file
        if (input.Contains("color.npy"))
        {
            // and peek at its header
            header = input.Peek("color.npy");
        }

        color = input.ReadUInt8("color.npy");
        gray  = input.ReadFloat32("gray.npy");
        input.Close();
    }
Ejemplo n.º 6
0
 static void NPZInputStreamInvalidFile()
 {
     var stream = new NPZInputStream(Test.AssetPath("uint8.npy"));
 }
Ejemplo n.º 7
0
 static void NPZInputStreamInvalidPath()
 {
     var stream = new NPZInputStream(Path.Combine("does_not_exist", "bad.npz"));
 }