public static Dictionary <string, Tensor> ReadFromFile(string filename) { var table = new Dictionary <string, Tensor>(); var file = File.Open(filename, FileMode.Open); var reader = new BinaryReader(file); // Read the shape list. var length = reader.ReadBEInt(); var shapes = ReadShapeInfoJson(reader.ReadBytes(length)); // Read the value table. length = reader.ReadBEInt(); var values = new float[length]; for (var i = 0; i < length / 4; i++) { values[i] = reader.ReadSingle(); } // Read and decode the weight table. length = reader.ReadBEInt(); // not used for (var i = 0; i < shapes.Length; i++) { var info = shapes[i]; length = info.shape.Aggregate(1, (acc, x) => acc * x); UnityEngine.Profiling.Profiler.BeginSample("Weight data decoding"); var bytes = reader.ReadBytes(length); var data = new float[length]; for (var j = 0; j < length; j++) { data[j] = values[bytes[j]]; } UnityEngine.Profiling.Profiler.EndSample(); var tensor = new Tensor(new Shape(info.shape), data); if (info.name.Contains("conv2d_transpose/kernel")) { var temp = new Tensor(new Shape( info.shape[0], info.shape[1], info.shape[3], info.shape[2] )); GpuBackend.InvokeReorderWeights(tensor, temp); Pix2Pix.GpuBackend.ExecuteAndClearCommandBuffer(); tensor.Dispose(); tensor = temp; } table[info.name] = tensor; } file.Close(); return(table); }
public static Dictionary <string, Tensor> ReadFromFile(string filename) { var table = new Dictionary <string, Tensor>(); var reader = new BinaryReader(File.Open(filename, FileMode.Open)); // Read the shape list. var length = reader.ReadBEInt(); var shapes = ReadShapeInfoJson(reader.ReadBytes(length)); // Read the value table. length = reader.ReadBEInt(); var values = new float[length]; for (var i = 0; i < length / 4; i++) { values[i] = reader.ReadSingle(); } // Read and decode the weight table. length = reader.ReadBEInt(); // not used for (var i = 0; i < shapes.Length; i++) { var info = shapes[i]; length = info.shape.Aggregate(1, (acc, x) => acc * x); var data = new float[length]; for (var j = 0; j < length; j++) { data[j] = values[reader.ReadByte()]; } var tensor = new Tensor(info.shape, data); if (info.name.Contains("conv2d_transpose/kernel")) { var temp = new Tensor(new[] { info.shape[0], info.shape[1], info.shape[3], info.shape[2] }); GpuBackend.InvokeReorderWeights(tensor, temp); tensor.Dispose(); tensor = temp; } table[info.name] = tensor; } return(table); }