示例#1
0
 public void Dispose()
 {
     if (Buffer != null)
     {
         GpuBackend.ReleaseBuffer(Buffer);
         Buffer = null;
     }
 }
示例#2
0
 void Dispose(bool disposing)
 {
     if (disposing && Buffer != null)
     {
         GpuBackend.ReleaseBuffer(Buffer);
         Buffer = null;
     }
 }
示例#3
0
        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);
        }
示例#4
0
 void OnDisable()
 {
     ReleaseBufferPool();
     if (_commandBuffer != null)
     {
         _commandBuffer.Dispose();
     }
     _instance = null;
 }
示例#5
0
        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);
            }
        }
示例#6
0
        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);
            }
        }
示例#7
0
        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);
        }
示例#8
0
        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);
            }
        }
示例#9
0
 public static void ConvertToTensor(Texture source, Tensor tensor)
 {
     tensor.Reset(new Shape(source.height, source.width, 3));
     GpuBackend.InvokeImageToTensor(source, tensor);
 }
示例#10
0
 public static void ConvertFromTensor(Tensor source, RenderTexture destination)
 {
     GpuBackend.InvokeTensorToImage(source, destination);
 }
示例#11
0
 public static void ConvertToTensor(Texture source, Tensor tensor)
 {
     GpuBackend.InvokeImageToTensor(source, tensor);
 }
示例#12
0
 void OnEnable()
 {
     _instance = this;
 }
示例#13
0
 void OnDestroy()
 {
     ReleaseBufferPool();
     _instance = null;
 }