|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text; |
| 8 | +using Tensorflow.Util; |
| 9 | + |
| 10 | +namespace Tensorflow.NumPy |
| 11 | +{ |
| 12 | + public partial class NumPyImpl |
| 13 | + { |
| 14 | + public NDArray load(string file) |
| 15 | + { |
| 16 | + using var stream = new FileStream(file, FileMode.Open); |
| 17 | + using var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true); |
| 18 | + int bytes; |
| 19 | + Type type; |
| 20 | + int[] shape; |
| 21 | + if (!ParseReader(reader, out bytes, out type, out shape)) |
| 22 | + throw new FormatException(); |
| 23 | + |
| 24 | + Array array = Create(type, shape.Aggregate((dims, dim) => dims * dim)); |
| 25 | + |
| 26 | + var result = new NDArray(ReadValueMatrix(reader, array, bytes, type, shape)); |
| 27 | + return result.reshape(shape); |
| 28 | + } |
| 29 | + |
| 30 | + bool ParseReader(BinaryReader reader, out int bytes, out Type t, out int[] shape) |
| 31 | + { |
| 32 | + bytes = 0; |
| 33 | + t = null; |
| 34 | + shape = null; |
| 35 | + |
| 36 | + // The first 6 bytes are a magic string: exactly "x93NUMPY" |
| 37 | + if (reader.ReadChar() != 63) return false; |
| 38 | + if (reader.ReadChar() != 'N') return false; |
| 39 | + if (reader.ReadChar() != 'U') return false; |
| 40 | + if (reader.ReadChar() != 'M') return false; |
| 41 | + if (reader.ReadChar() != 'P') return false; |
| 42 | + if (reader.ReadChar() != 'Y') return false; |
| 43 | + |
| 44 | + byte major = reader.ReadByte(); // 1 |
| 45 | + byte minor = reader.ReadByte(); // 0 |
| 46 | + |
| 47 | + if (major != 1 || minor != 0) |
| 48 | + throw new NotSupportedException(); |
| 49 | + |
| 50 | + ushort len = reader.ReadUInt16(); |
| 51 | + |
| 52 | + string header = new String(reader.ReadChars(len)); |
| 53 | + string mark = "'descr': '"; |
| 54 | + int s = header.IndexOf(mark) + mark.Length; |
| 55 | + int e = header.IndexOf("'", s + 1); |
| 56 | + string type = header.Substring(s, e - s); |
| 57 | + bool? isLittleEndian; |
| 58 | + t = GetType(type, out bytes, out isLittleEndian); |
| 59 | + |
| 60 | + if (isLittleEndian.HasValue && isLittleEndian.Value == false) |
| 61 | + throw new Exception(); |
| 62 | + |
| 63 | + mark = "'fortran_order': "; |
| 64 | + s = header.IndexOf(mark) + mark.Length; |
| 65 | + e = header.IndexOf(",", s + 1); |
| 66 | + bool fortran = bool.Parse(header.Substring(s, e - s)); |
| 67 | + |
| 68 | + if (fortran) |
| 69 | + throw new Exception(); |
| 70 | + |
| 71 | + mark = "'shape': ("; |
| 72 | + s = header.IndexOf(mark) + mark.Length; |
| 73 | + e = header.IndexOf(")", s + 1); |
| 74 | + shape = header.Substring(s, e - s).Split(',').Where(v => !String.IsNullOrEmpty(v)).Select(Int32.Parse).ToArray(); |
| 75 | + |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + Type GetType(string dtype, out int bytes, out bool? isLittleEndian) |
| 80 | + { |
| 81 | + isLittleEndian = IsLittleEndian(dtype); |
| 82 | + bytes = Int32.Parse(dtype.Substring(2)); |
| 83 | + |
| 84 | + string typeCode = dtype.Substring(1); |
| 85 | + |
| 86 | + if (typeCode == "b1") |
| 87 | + return typeof(bool); |
| 88 | + if (typeCode == "i1") |
| 89 | + return typeof(Byte); |
| 90 | + if (typeCode == "i2") |
| 91 | + return typeof(Int16); |
| 92 | + if (typeCode == "i4") |
| 93 | + return typeof(Int32); |
| 94 | + if (typeCode == "i8") |
| 95 | + return typeof(Int64); |
| 96 | + if (typeCode == "u1") |
| 97 | + return typeof(Byte); |
| 98 | + if (typeCode == "u2") |
| 99 | + return typeof(UInt16); |
| 100 | + if (typeCode == "u4") |
| 101 | + return typeof(UInt32); |
| 102 | + if (typeCode == "u8") |
| 103 | + return typeof(UInt64); |
| 104 | + if (typeCode == "f4") |
| 105 | + return typeof(Single); |
| 106 | + if (typeCode == "f8") |
| 107 | + return typeof(Double); |
| 108 | + if (typeCode.StartsWith("S")) |
| 109 | + return typeof(String); |
| 110 | + |
| 111 | + throw new NotSupportedException(); |
| 112 | + } |
| 113 | + |
| 114 | + bool? IsLittleEndian(string type) |
| 115 | + { |
| 116 | + bool? littleEndian = null; |
| 117 | + |
| 118 | + switch (type[0]) |
| 119 | + { |
| 120 | + case '<': |
| 121 | + littleEndian = true; |
| 122 | + break; |
| 123 | + case '>': |
| 124 | + littleEndian = false; |
| 125 | + break; |
| 126 | + case '|': |
| 127 | + littleEndian = null; |
| 128 | + break; |
| 129 | + default: |
| 130 | + throw new Exception(); |
| 131 | + } |
| 132 | + |
| 133 | + return littleEndian; |
| 134 | + } |
| 135 | + |
| 136 | + Array Create(Type type, int length) |
| 137 | + { |
| 138 | + // ReSharper disable once PossibleNullReferenceException |
| 139 | + while (type.IsArray) |
| 140 | + type = type.GetElementType(); |
| 141 | + |
| 142 | + return Array.CreateInstance(type, length); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments