public static Tensor Deconv2D(Tensor input, Tensor filter, Tensor bias) { var channels = filter.Shape[3]; var kernel = "TransConv2D_" + (channels == 3 ? "final" : channels.ToString()); return(GpuHelper.InvokeConvolutionKernel(GpuHelper.ConvolutionMode.Up, kernel, input, filter, bias)); }
public static Tensor Deconv2D(Tensor input, Tensor filter, Tensor bias) { #if ENABLE_COMPUTE var outChannels = filter.Shape[2]; var kernel = outChannels >= 512 ? "Deconv2D_512_1_1" : "Deconv2D_64_16_1"; if (outChannels == 3) { kernel = "Deconv2D_3_256_1"; } return(GpuHelper.InvokeConvolutionKernel(GpuHelper.ConvolutionMode.Backward, kernel, input, filter, bias)); #else var inHeight = input.Shape[0]; var inWidth = input.Shape[1]; var inChannels = input.Shape[2]; var outHeight = inHeight * 2; var outWidth = inWidth * 2; var outChannels = filter.Shape[2]; var filterHeight = filter.Shape[0]; var filterWidth = filter.Shape[1]; var output = new Tensor(new [] { outHeight, outWidth, outChannels }); for (var oc = 0; oc < outChannels; oc++) { for (var oy = 0; oy < outHeight; oy++) { var ymin = (oy - 1) / 2; for (var ox = 0; ox < outWidth; ox++) { var xmin = (ox - 1) / 2; var prod = 0.0f; for (var fy = oy % 2; fy < filterHeight; fy += 2) { for (var fx = ox % 2; fx < filterWidth; fx += 2) { for (var ic = 0; ic < inChannels; ic++) { var pixel = input.Get(ymin + fy / 2, xmin + fx / 2, ic); var weight = filter.Get( filterHeight - 1 - fy, filterWidth - 1 - fx, oc, ic ); prod += pixel * weight; } } } output.Set(oy, ox, oc, prod + bias.Get(oc)); } } } return(output); #endif }
public static Tensor Tanh(Tensor input) { var output = new Tensor(input.Shape); GpuHelper.InvokeActivationKernel("Tanh", input, output); return(output); }
public static Tensor BatchNorm(Tensor input, Tensor scale, Tensor offset) { var channels = scale.Shape[0]; var kernel = channels == 512 ? "BatchNorm512" : "BatchNorm64"; return(GpuHelper.InvokeBatchNormKernel(kernel, input, scale, offset)); }
public static Tensor Conv2D(Tensor input, Tensor filter, Tensor bias) { var outChannels = filter.Shape[3]; var kernel = outChannels >= 512 ? "Conv2D_512_1" : "Conv2D_64_8"; return(GpuHelper.InvokeConvolutionKernel(GpuHelper.ConvolutionMode.Down, kernel, input, filter, bias)); }
public static Tensor LeakyRelu(Tensor input, float alpha) { var output = new Tensor(input.Shape); ComputeAssets.Activation.SetFloat("Alpha", alpha); GpuHelper.InvokeActivationKernel("LeakyRelu", input, output); return(output); }
public static Tensor Concat(Tensor input1, Tensor input2) { var elements = input1.Shape[0] * input1.Shape[1]; var kernel = elements < 512 ? "Concat64" : "Concat512"; if (elements < 64) { kernel = "Concat4"; } return(GpuHelper.InvokeConcatKernel(kernel, input1, input2)); }
public static Tensor Deconv2D(Tensor input, Tensor filter, Tensor bias) { var outChannels = filter.Shape[2]; var kernel = outChannels >= 512 ? "TransConv2D_512_1" : "TransConv2D_64_8"; if (outChannels == 3) { kernel = "TransConv2D_3_128"; } return(GpuHelper.InvokeConvolutionKernel(GpuHelper.ConvolutionMode.Up, kernel, input, filter, bias)); }
public static Tensor Conv2D(Tensor input, Tensor filter, Tensor bias) { #if ENABLE_COMPUTE var outChannels = filter.Shape[3]; var kernel = outChannels >= 512 ? "Conv2D_512_1_1" : "Conv2D_64_16_1"; return(GpuHelper.InvokeConvolutionKernel(GpuHelper.ConvolutionMode.Forward, kernel, input, filter, bias)); #else var inHeight = input.Shape[0]; var inWidth = input.Shape[1]; var inChannels = input.Shape[2]; var outHeight = inHeight / 2; var outWidth = inWidth / 2; var outChannels = filter.Shape[3]; var filterHeight = filter.Shape[0]; var filterWidth = filter.Shape[1]; var output = new Tensor(new [] { outHeight, outWidth, outChannels }); for (var oc = 0; oc < outChannels; oc++) { for (var oy = 0; oy < outHeight; oy++) { var ymin = oy * 2 - filterHeight / 2 + 1; for (var ox = 0; ox < outWidth; ox++) { var xmin = ox * 2 - filterWidth / 2 + 1; var prod = 0.0f; for (var fy = 0; fy < filterHeight; fy++) { for (var fx = 0; fx < filterWidth; fx++) { for (var ic = 0; ic < inChannels; ic++) { var pixel = input.Get(ymin + fy, xmin + fx, ic); var weight = filter.Get(fy, fx, ic, oc); prod += pixel * weight; } } } output.Set(oy, ox, oc, prod + bias.Get(oc)); } } } return(output); #endif }
public static Tensor Tanh(Tensor input) { #if ENABLE_COMPUTE return(GpuHelper.InvokeFunctionKernel("Tanh", input)); #else var data = new float[input.Data.Length]; for (var i = 0; i < data.Length; i++) { data[i] = (float)System.Math.Tanh(input.Data[i]); } return(new Tensor(input.Shape, data)); #endif }
void Dispose(bool disposing) { if (disposing) { Shape = null; if (Buffer != null) { GpuHelper.ReleaseBuffer(Buffer); Buffer = null; } } }
public static Tensor BatchNorm(Tensor input, Tensor scale, Tensor offset) { #if ENABLE_COMPUTE var channels = scale.Shape[0]; var kernel = channels == 512 ? "BatchNorm512" : "BatchNorm64"; return(GpuHelper.InvokeNormalizationKernel(kernel, input, scale, offset)); #else UnityEngine.Debug.Assert(input.Shape.Length == 3); var output = new Tensor(input.Shape); var epsilon = 1e-5f; for (var ch = 0; ch < input.Shape[2]; ch++) { var mean = 0.0f; for (var y = 0; y < input.Shape[0]; y++) { for (var x = 0; x < input.Shape[1]; x++) { mean += input.Get(y, x, ch); } } mean /= input.Shape[0] * input.Shape[1]; var variance = 0.0f; for (var y = 0; y < input.Shape[0]; y++) { for (var x = 0; x < input.Shape[1]; x++) { variance += MathUtil.Square(input.Get(y, x, ch) - mean); } } variance /= input.Shape[0] * input.Shape[1]; var offs = offset.Get(ch); var sc = scale.Get(ch); sc /= UnityEngine.Mathf.Sqrt(variance + epsilon); for (var y = 0; y < input.Shape[0]; y++) { for (var x = 0; x < input.Shape[1]; x++) { output.Set(y, x, ch, offs + (input.Get(y, x, ch) - mean) * sc); } } } return(output); #endif }
public Tensor(int[] shape, float[] data = null) { Shape = shape; var total = shape.Aggregate(1, (acc, x) => acc * x); Buffer = GpuHelper.AllocateBuffer(total); if (data != null) { Debug.Assert(data.Length == total); Buffer.SetData(data); } }
public static Tensor Relu(Tensor input) { #if ENABLE_COMPUTE return(GpuHelper.InvokeFunctionKernel("Relu", input)); #else var data = new float[input.Data.Length]; for (var i = 0; i < data.Length; i++) { var v = input.Data[i]; data[i] = v < 0 ? 0 : v; } return(new Tensor(input.Shape, data)); #endif }
public static Tensor LeakyRelu(Tensor input, float alpha) { #if ENABLE_COMPUTE Pix2PixResources.Compute.SetFloat("Alpha", alpha); return(GpuHelper.InvokeFunctionKernel("LeakyRelu", input)); #else var data = new float[input.Data.Length]; for (var i = 0; i < data.Length; i++) { var v = input.Data[i]; data[i] = v < 0 ? v * alpha : v; } return(new Tensor(input.Shape, data)); #endif }
void OnDestroy() { GpuHelper.ReleaseAllBuffers(); Destroy(_sourceTexture); Destroy(_resultTexture); Destroy(_lineMaterial); Destroy(_lineMesh); Destroy(_eraserMaterial); Destroy(_eraserMesh); WeightReader.DisposeTable(_weightTable); _sourceTensor.Dispose(); _resultTensor.Dispose(); }
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()]; } table[info.name] = new Tensor(info.shape, data); if (info.name.Contains("conv2d_transpose/kernel")) { var t = table[info.name]; table[info.name] = GpuHelper.SwapFilter(t); t.Dispose(); } } return(table); }
public static Tensor BatchNorm(Tensor input, Tensor scale, Tensor offset) { var kernel = "BatchNorm" + scale.Shape[0]; return(GpuHelper.InvokeBatchNormKernel(kernel, input, scale, offset)); }
public static Tensor Concat(Tensor input1, Tensor input2) { return(GpuHelper.InvokeConcatKernel(input1, input2)); }
public static Tensor Tanh(Tensor input) { return(GpuHelper.InvokeActivationKernel("Tanh", input)); }
public static Tensor LeakyRelu(Tensor input, float alpha) { ComputeAssets.Activation.SetFloat("Alpha", alpha); return(GpuHelper.InvokeActivationKernel("LeakyRelu", input)); }
public static Tensor Relu(Tensor input) { return(GpuHelper.InvokeActivationKernel("Relu", input)); }
public static Tensor Deconv2D(Tensor input, Tensor filter, Tensor bias) { var kernel = "TransConv2D_" + filter.Shape[2]; return(GpuHelper.InvokeConvolutionKernel(GpuHelper.ConvolutionMode.Up, kernel, input, filter, bias)); }
public static Tensor Conv2D(Tensor input, Tensor filter, Tensor bias) { var kernel = "Conv2D_" + filter.Shape[3]; return(GpuHelper.InvokeConvolutionKernel(GpuHelper.ConvolutionMode.Down, kernel, input, filter, bias)); }
public static void Tanh(Tensor input, Tensor output) { GpuHelper.InvokeActivationKernel("Tanh", input, output); }
public static Tensor BatchNorm(Tensor input, Tensor scale, Tensor offset) { return(GpuHelper.InvokeBatchNormKernel(input, scale, offset)); }