private void computeProbabilityForPosition(int positionX, int positionY, int computedIndex, out float probability, out float feebackProbability, out float outputProbability)
        {
            int sum = 0;
            for (int i = 0; i < numberOfContexts; i++)
            {
                sum += contextTableMap[i][computedIndexes[computedIndex, i] & tableSizeMasks[i]];
            }

            probability = LogisticHelper.squash(sum * outputProbabilitySquashFactor);
            int sseContext = currentInputImage.gray[positionY, positionX];
            feebackProbability = sse.getEstimation(sseContext, probability);
            //int sse1Context = NumberUtils.hash3(currentInputImage.r[positionY, positionX], currentInputImage.g[positionY, positionX], currentInputImage.b[positionY, positionX]) & 65535;
            //outputProbability = sse1.getEstimation(sse1Context, feebackProbability);
            outputProbability = feebackProbability;
        }
示例#2
0
        public SecondarySymbolEstimationStretchInput(int numberOfContexts, int numberOfInterpolationIntervals, float learningRate)
        {
            this.numberOfInterpolationIntervals = numberOfInterpolationIntervals;
            this.learningRate = learningRate;
            estimators        = new float[numberOfContexts, numberOfInterpolationIntervals + 1];
            for (int i = 0; i < numberOfContexts; i++)
            {
                for (int j = 0; j <= numberOfInterpolationIntervals; j++)
                {
                    float p = (float)j / numberOfInterpolationIntervals;
                    estimators[i, j] = LogisticHelper.squash((2 * LogisticHelper.squashAbsoluteMaximumValue) * p - LogisticHelper.squashAbsoluteMaximumValue);
                }
            }

            intervalWeightFactor = numberOfInterpolationIntervals / (2 * LogisticHelper.squashAbsoluteMaximumValue);
        }
        private void updateProbability(int positionX, int positionY, float groundTruthProbability, float probability, float feebackProbability, int computedIndex)
        {
            int sseContext = currentInputImage.gray[positionY, positionX];
            sse.update(sseContext, probability, groundTruthProbability);
            //sse1.update(groundTruthProbability);

#if useEntropyLoss
            float globalError = (feebackProbability - groundTruthProbability) * globalErrorWeight; // entropy loss
#else
            float globalError = (feebackProbability - groundTruthProbability) * feebackProbability * (1 - feebackProbability) * globalErrorWeight; // square loss
#endif
            for (int i = 0; i < numberOfContexts; i++)
            {
                int tableValue;
                short? contextValue = contextTableMap[i].getContextValue(computedIndexes[computedIndex, i]);
                if (contextValue != null)
                {
                    tableValue = contextValue.Value;
                }
                else
                {
                    tableValue = 0;
                }
#if useEntropyLoss
                float localError = (LogisticHelper.squash(tableValue * tableValueSquashFactor) - groundTruthProbability) * localErrorWeight; // entropy loss
#else
                float localProbability = LogisticHelper.squash(tableValue * tableValueSquashFactor);
                float localError = (localProbability - groundTruthProbability) * localProbability * (1 - localProbability) * localErrorWeight; // square loss
#endif
                tableValue = tableValue - (int)(floatUnit * (globalError + localError) + 0.5f);
                if (tableValue > short.MaxValue)
                {
                    tableValue = short.MaxValue;
                }
                else
                {
                    if (tableValue < short.MinValue)
                    {
                        tableValue = short.MinValue;
                    }
                }
                contextTableMap[i].updateLastContextValue((short)tableValue);
            }
        }
        public float train(List <ImageDescription> inputImages, ImageDescription inputImageGroundTruth)
        {
            int newSizeX, newSizeY;
            List <ImageDescription> imagesToBlend;

            ImageDescriptionUtil.makeAllImagesSameSize(inputImages, out newSizeX, out newSizeY, out imagesToBlend);

            float entropyLoss = 0;

            for (int i = 0; i < newSizeY; i++)
            {
                for (int j = 0; j < newSizeX; j++)
                {
                    float stretchedProbability = computePerPixelStretchedProbability(imagesToBlend, i, j);
                    float probability          = LogisticHelper.squash(stretchedProbability);
                    if (probability < LogisticHelper.probabilityMinValue)
                    {
                        probability = LogisticHelper.probabilityMinValue;
                    }
                    else
                    {
                        if (probability > LogisticHelper.probabilityMaxValue)
                        {
                            probability = LogisticHelper.probabilityMaxValue;
                        }
                    }
                    float groundTruthProbability = groundTruthProbabilityCache[inputImageGroundTruth.gray[i, j]];
                    entropyLoss += LogisticHelper.computeEntropyLoss(probability, groundTruthProbability);

#if useEntropyLoss
                    float loss = groundTruthProbability - probability;
#else
                    float loss = (groundTruthProbability - probability) * probability * (1 - probability);
#endif
                    for (int imageIndex = 0; imageIndex < imagesToBlend.Count; imageIndex++)
                    {
                        byte pixelValue = imagesToBlend[imageIndex].gray[i, j];
                        weights[imageIndex] += learningConstant * stretchedPixelValueCache[pixelValue] * loss;
                    }
                }
            }
            return(entropyLoss);
        }
        private void computeProbabilityForPosition(int positionX, int positionY, int computedIndex, out float probability, out float feebackProbability, out float outputProbability)
        {
            int sum = 0;
            int numberOfHits = 0;
            for (int i = 0; i < numberOfContexts; i++)
            {
                short? contextValue = contextTableMap[i].getContextValue(computedIndexes[computedIndex, i]);
                if (contextValue != null)
                {
                    sum += contextValue.Value;
                    numberOfHits++;
                }
            }

            probability = LogisticHelper.squash(sum * outputProbabilitySquashFactor / ((numberOfContexts + numberOfHits + 1) >> 1));
            int sseContext = currentInputImage.gray[positionY, positionX];
            feebackProbability = sse.getEstimation(sseContext, probability);
            //int sse1Context = NumberUtils.hash3(currentInputImage.r[positionY, positionX], currentInputImage.g[positionY, positionX], currentInputImage.b[positionY, positionX]) & 65535;
            //outputProbability = sse1.getEstimation(sse1Context, feebackProbability);
            outputProbability = feebackProbability;
        }
        public ImageDescription blendImages(List <ImageDescription> images)
        {
            int newSizeX, newSizeY;
            List <ImageDescription> imagesToBlend;

            ImageDescriptionUtil.makeAllImagesSameSize(images, out newSizeX, out newSizeY, out imagesToBlend);

            ImageDescription output = new ImageDescription();

            output.sizeX     = newSizeX;
            output.sizeY     = newSizeY;
            output.grayscale = true;

            byte[,] outputGray = new byte[newSizeY, newSizeX];

            for (int i = 0; i < newSizeY; i++)
            {
                for (int j = 0; j < newSizeX; j++)
                {
                    float stretchedProbability = computePerPixelStretchedProbability(imagesToBlend, i, j);
                    float probability          = LogisticHelper.squash(stretchedProbability);
                    if (probability < LogisticHelper.probabilityMinValue)
                    {
                        probability = LogisticHelper.probabilityMinValue;
                    }
                    else
                    {
                        if (probability > LogisticHelper.probabilityMaxValue)
                        {
                            probability = LogisticHelper.probabilityMaxValue;
                        }
                    }
                    outputGray[i, j] = (byte)(probability * 255.0f + 0.5f);
                }
            }
            output.gray = outputGray;
            return(output);
        }