public void Dispose() { if (Buffer != null) { GpuBackend.ReleaseBuffer(Buffer); Buffer = null; } }
void Dispose(bool disposing) { if (disposing && Buffer != null) { GpuBackend.ReleaseBuffer(Buffer); Buffer = null; } }
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); }
void OnDisable() { ReleaseBufferPool(); if (_commandBuffer != null) { _commandBuffer.Dispose(); } _instance = null; }
public Tensor(Shape shape, float[] data = null) { Shape = shape; var total = shape.ElementCount; Buffer = GpuBackend.AllocateBuffer(total); if (data != null) { UnityEngine.Debug.Assert(data.Length == total); Buffer.SetData(data); } }
public Tensor(int[] shape, float[] data = null) { Shape = shape; var total = shape.Aggregate(1, (acc, x) => acc * x); Buffer = GpuBackend.AllocateBuffer(total); if (data != null) { UnityEngine.Debug.Assert(data.Length == total); Buffer.SetData(data); } }
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); }
public void Reset(Shape shape) { Shape = shape; var total = shape.ElementCount; if (Buffer != null && total != Buffer.count) { GpuBackend.ReleaseBuffer(Buffer); Buffer = null; } if (Buffer == null && total > 0) { Buffer = GpuBackend.AllocateBuffer(total); } }
public static void ConvertToTensor(Texture source, Tensor tensor) { tensor.Reset(new Shape(source.height, source.width, 3)); GpuBackend.InvokeImageToTensor(source, tensor); }
public static void ConvertFromTensor(Tensor source, RenderTexture destination) { GpuBackend.InvokeTensorToImage(source, destination); }
public static void ConvertToTensor(Texture source, Tensor tensor) { GpuBackend.InvokeImageToTensor(source, tensor); }
void OnEnable() { _instance = this; }
void OnDestroy() { ReleaseBufferPool(); _instance = null; }