private void RemoveAllPooled(int id) { if (pooledGobjects == null || pooledGobjects.Count == 0) { return; } for (int i = pooledGobjects.Count - 1; i > -1; i--) { PoolingType poolingType = pooledGobjects[i]; if (poolingType.id == id) { if (poolingType.gameobject != null) { if (poolingType.gameobject.isReady) { GameObject.Destroy(poolingType.gameobject.gameObject); } else { poolingType.gameobject.DestroyWhenDone = true; } } pooledGobjects.RemoveAt(i); } } }
public CNNLayer(int numFeatureMaps, int numPrevLayerFeatureMaps, int inputSize, int kernelSize, PoolingType poolingType, ActivationType activationType, int batchSize) { this.batchSize = batchSize; this.ConvolSums = new Matrix[batchSize, numFeatureMaps]; this.kernelSize = kernelSize; this.numFeatureMaps = numFeatureMaps; this.numPrevLayerFeatureMaps = numPrevLayerFeatureMaps; this.ConvolResults = new Matrix[batchSize, numPrevLayerFeatureMaps, numFeatureMaps]; int convOutputSize = inputSize - kernelSize + 1; for (int i = 0; i < batchSize; i++) { for (int j = 0; j < numFeatureMaps; j++) { ConvolSums[i, j] = new Matrix(convOutputSize, convOutputSize); } } Kernels = new Matrix[numPrevLayerFeatureMaps, numFeatureMaps]; KernelGrads = new Matrix[numPrevLayerFeatureMaps, numFeatureMaps]; InitMatrix2DArray(ref Kernels, numPrevLayerFeatureMaps, numFeatureMaps, kernelSize); InitMatrix2DArray(ref KernelGrads, numPrevLayerFeatureMaps, numFeatureMaps, kernelSize); InitializeKernels(); for (int i = 0; i < numFeatureMaps; i++) { FeatureMap fmp = new FeatureMap(convOutputSize, poolingType, activationType, batchSize); FeatureMapList.Add(fmp); } }
/// <summary> /// Adds a pooling layer to the network. /// </summary> /// <param name="poolingDimension"> /// A side length of the pool (all pools are square, so if you want a 5x5 pool this parameter /// should be 5) /// </param> /// <param name="stride">The stride used for the pool.</param> /// <param name="poolingType">The type of pooling to use.</param> public void AddPoolingLayer(int poolingDimension, int stride, PoolingType poolingType) { this.LayerInformation.Add(new PoolingLayerInformation { KernelSize = poolingDimension, PoolingType = poolingType, Stride = stride }); }
/// <summary> /// Creates a pulling layer for a two-dimensional vector. If the previous layer has a non-two-dimensional output, an exception is thrown /// </summary> /// <param name="poolingWindowWidth">Pulling window width</param> /// <param name="poolingWindowHeight">Pulling window height</param> /// <param name="hStride">Horizontal displacement step of the pulling window (according to matrix columns)</param> /// <param name="vStride">Step of shifting the pulling window vertically (along the rows of the matrix)</param> /// <param name="poolingType">Type of pulling. Maximum or medium</param> /// <param name="name"></param> public Pooling2D(int poolingWindowWidth, int poolingWindowHeight, int hStride, int vStride, PoolingType poolingType, string name = "Pooling2D") { _poolingWindowWidth = poolingWindowWidth; _poolingWindowHeight = poolingWindowHeight; _hStride = hStride; _vStride = vStride; _poolingType = poolingType; _name = name; }
public _Pooling(int[] pool_size, int[] strides, int[] padding, bool ceil_mode, bool global_pool, PoolingType pool_type, string layout, bool?count_include_pad = null, string prefix = null, ParameterDict @params = null) : base(prefix, @params) { Kernel = pool_size; Strides = strides ?? pool_size; Padding = padding; GlobalPool = global_pool; Layout = layout; CountIncludePad = count_include_pad; }
{ // featuremap for layer2 in cnn and onwards public FeatureMap(int inputDataSize, PoolingType poolingType, ActivationType activationType, int batchSize) { this.inputDataSize = inputDataSize; this.poolingType = poolingType; this.activationType = activationType; this.batchSize = batchSize; DeltaSS = new Matrix[batchSize]; DeltaCV = new Matrix[batchSize]; OutPutSS = new Matrix[batchSize]; ActCV = new Matrix[batchSize]; APrime = new Matrix[batchSize]; Sum = new Matrix[batchSize]; }
public Function Pooling1D(Variable x, int pShape, DataType dType, PoolingType pType, DeviceDescriptor device, string name = "", uint seed = 1) { if (pShape == 0) { throw new Exception("Pooling shape is not in correct size!"); } // var shape = new int[] { pShape }; //create pooling layer var rtrn = CNTKLib.Pooling(x, pType, shape); return(rtrn); }
private static OneArgumentModule Pooling(PoolingType poolingType, int kernelSize, int rank = 1, int stride = 1, bool padding = false, bool ceilMode = false, string name = "") { int[] kernelShape = new int[rank]; int[] strides = new int[rank]; bool[] autoPadding = new bool[rank]; for (int i = 0; i < rank; i++) { kernelShape[i] = kernelSize; strides[i] = stride; autoPadding[i] = padding; } return(x => C.Pooling(x, poolingType, kernelShape, strides, new BoolVector(autoPadding), ceilMode, padding, name)); }
public void RemovePrefab(int id) { if (list_Pooling == null || list_Pooling.Count == 0) { return; } for (int i = list_Pooling.Count - 1; i > -1; i--) { PoolingType poolingType = list_Pooling[i]; if (poolingType.id == id) { list_Pooling.RemoveAt(i); hihi[poolingType.id] = false; } } RemoveAllPooled(id); }
public byte[] GetEigenvalue(Bitmap bitmap, PoolingType poolingType, Color threshold, int precisionX = 16, int precisionY = 16) { InputDataType = DataType.Image; var pool = new Pooling(InputDataType); var poolColor = pool.Execute(bitmap, precisionX, precisionY, poolingType); _compressionPoint = new Point(poolColor.GetLength(0), poolColor.GetLength(1)); _poolColor = poolColor; if (threshold == Color.Black) { threshold = pool.GetMedianColor(); } var binary = new Binarization(); var eigenvalue = binary.Execute(poolColor, threshold); SimilarArray = binary.GetSimilarArray(); return(eigenvalue); }
public byte[] GetEigenvalue(float[,] data, PoolingType poolingType, int precisionX = 16, int precisionY = 16, float threshold = 9999) { InputDataType = DataType.Data; var pool = new Pooling(InputDataType); var poolData = pool.Execute(data, precisionX, precisionY, poolingType); _compressionPoint = new Point(poolData.GetLength(0), poolData.GetLength(1)); _poolData = poolData; if (threshold == 9999) { threshold = pool.GetMedianValue(); } var binary = new Binarization(); var eigenvalue = binary.Execute(poolData, threshold); SimilarArray = binary.GetSimilarArray(); return(eigenvalue); }
internal float[,] Execute(float[,] data, int compressionWidth, int compressionHeight, PoolingType poolingType) { _width = data.GetLength(0); _height = data.GetLength(1); if (_width < compressionWidth) { return(null); } if (_height < compressionHeight) { return(null); } // 倍数 var multipleX = _width / compressionWidth; var multipleY = _height / compressionHeight; var cWidth = _width / multipleX; var cHeight = _height / multipleY; var result = new float[cWidth, cHeight]; for (int cx = 0; cx < cWidth; cx++) { for (int cy = 0; cy < cHeight; cy++) { if (poolingType == PoolingType.Mean) { result[cx, cy] = PoolMean(data, cx * multipleX, multipleX, cy * multipleY, multipleY); } else { result[cx, cy] = PoolMax(data, cx * multipleX, multipleX, cy * multipleY, multipleY); } } } return(result); }
/// <summary> /// Adds a pulling layer for a two-dimensional vector. If the previous layer has a non-two-dimensional output, an exception is thrown /// </summary> /// <param name="poolingWindowWidth">Pulling window width</param> /// <param name="poolingWindowHeight">Pulling window height</param> /// <param name="hStride">Horizontal displacement step of the pulling window (according to matrix columns)</param> /// <param name="vStride">Step of shifting the pulling window vertically (along the rows of the matrix)</param> /// <param name="poolingType">Type of pulling. Maximum or medium</param> /// <param name="name"></param> public static Function Build(Variable input, int poolingWindowWidth, int poolingWindowHeight, int hStride, int vStride, PoolingType poolingType, string name) { var pooling = CNTKLib.Pooling(input, poolingType, new int[] { poolingWindowWidth, poolingWindowHeight }, new int[] { hStride, vStride }, new bool[] { true }); return(CNTKLib.Alias(pooling, name)); }
internal byte[,,] Execute(Bitmap bitmap, int compressionWidth, int compressionHeight, PoolingType poolingType) { _width = bitmap.Width; _height = bitmap.Height; if (_width < compressionWidth) { return(null); } if (_height < compressionHeight) { return(null); } // 倍数 var multipleX = _width / compressionWidth; var multipleY = _height / compressionHeight; var cWidth = (int)Math.Ceiling(_width / (float)multipleX); var cHeight = (int)Math.Ceiling(_height / (float)multipleY); var result = new byte[cWidth, cHeight, 4]; var lockbmp = new LockBitmap4Pointer(bitmap); lockbmp.LockBits(); Parallel.For(0, cWidth, (cx) => { //for (int cx = 0; cx < cWidth; cx++) //{ Parallel.For(0, cHeight, (cy) => { //for (int cy = 0; cy < cHeight; cy++) //{ (byte a, byte r, byte g, byte b)data; if (poolingType == PoolingType.Mean) { data = PoolMean3(lockbmp, cx * multipleX, multipleX, cy * multipleY, multipleY); } else { data = PoolMax3(lockbmp, cx * multipleX, multipleX, cy * multipleY, multipleY); } result[cx, cy, 0] = data.a; result[cx, cy, 1] = data.r; result[cx, cy, 2] = data.g; result[cx, cy, 3] = data.b; }); }); //从内存解锁Bitmap lockbmp.UnlockBits(); return(result); }
/// <summary> /// create a pooling function /// </summary> /// <param name="operand">input</param> /// <param name="poolingType">pooling type</param> /// <param name="poolingWindowShape">pooiling window dimensions</param> /// <param name="strides">strides to apply the pooling</param> /// <param name="autoPadding"></param> /// <returns></returns> public static Function Pooling(Variable operand, PoolingType poolingType, NDShape poolingWindowShape, NDShape strides, IEnumerable <bool> autoPadding) { BoolVector autoPaddingVector = Helper.AsBoolVector(autoPadding); return(Pooling(operand, poolingType, poolingWindowShape, strides, autoPaddingVector)); }
public override void Load(XmlElement layer) { Filter = layer.GetAttribute("filter").DeserializeAsPoolFilter(); Stride = layer.GetAttribute("stride").DeserializeAsPoolStride(); Type = (PoolingType)Enum.Parse(typeof(PoolingType), layer.GetAttribute("pooling-type")); }
public void AddLayer( LayerType layerType, int layerOrder = -1, DataShape shape = null, /*Convolution parameters*/ int convolution_numberOfKernels = 64, int convolution_kernelSize = 3, bool convolution_preserveSizeAfterConvolution = true, /*Pooling parameters*/ PoolingType poolingType = PoolingType.Max, /*UnPooling Layer parameters*/ UnPoolingType unPoolingType = UnPoolingType.ExactLocation, /*Fully Connected parameters*/ int fullyConnected_numberOfNeurons = 512, ActivationType activationType = ActivationType.RelU, float activationThreshold = 0.5f) { /*Validate if Input layer exist. This layer is added manually - before adding any other layers. It's important to define the Size of the Data. Input layer should with order = 1*/ if (layerType == LayerType.Input) { /*Shape should be specified when calling 'AddLayer' function*/ this.layers.Add(new LayerInput(shape)); } else if (ValidateInputLayer()) { /*If layer order is not provided -get last layer and increase with 1*/ if (layerOrder == -1) { layerOrder = this.layers[this.layers.Count - 1].layerOrder + 1; } /*Get previous layer Datashape*/ var previousLayerShape = (from t in this.layers where t.layerOrder == layerOrder - 1 select t.GetOutputShape()).FirstOrDefault(); /*Get number of inputs*/ var inputs = this.GetInputs(layerOrder); var numberOfItems_previousLayer = inputs.Count;//GetnumberOfInputs(layerOrder); /*Get previous layer dataShape*/ /*Add layer -connect neurons with synapses from previous layer to the current one*/ for (int i = 0; i < numberOfItems_previousLayer; i++) { Layer currentLayer = null; switch (layerType) { case LayerType.Convolution2D: currentLayer = new LayerConvolution( layerOrder, convolution_numberOfKernels, convolution_kernelSize, convolution_preserveSizeAfterConvolution, previousLayerShape, activationType, activationThreshold); break; case LayerType.Pooling: /*Construct new Pooling layer*/ break; } this.ConnectLayerToPrevious(inputs[i], currentLayer); this.layers.Add(currentLayer); } } }
private static OneArgumentModule Pooling(PoolingType poolingType, int [] kernelShape, int [] strides, bool [] paddings, bool ceilMode = false, string name = "") { return(x => C.Pooling(x, poolingType, kernelShape, strides, new BoolVector(paddings), ceilMode, true, name)); }
public Function Pooling2D(Variable x, int[] pShape, int stride, DataType dType, PoolingType pType, DeviceDescriptor device, string name = "", uint seed = 1) { if (pShape == null || pShape.Length < 2) { throw new Exception("Pooling shape is not in correct size!"); } // var shape = new int[] { pShape[0], pShape[1] }; var strides = new int[] { stride }; //create convolution layer var rtrn = CNTKLib.Pooling(x, pType, shape, strides); return(rtrn); }