private void ComputeFilteredImage()
        {
            FilteredImage image = (FilteredImage)PreviousLayer.GetData();

            FilteredImageChannel[] newChannels = new FilteredImageChannel[image.NumberOfChannels];
            int size = image.Size;

            for (int i = 0; i < image.NumberOfChannels; i++)
            {
                double[,] newValues = new double[size, size];

                for (int valuesI = 0; valuesI < size; valuesI++)
                {
                    for (int valuesJ = 0; valuesJ < size; valuesJ++)
                    {
                        if (GlobalRandom.GetRandomDouble() < Rate)
                        {
                            newValues[valuesI, valuesJ] = 0;
                        }
                        else
                        {
                            newValues[valuesI, valuesJ] = image.Channels[i].Values[valuesI, valuesJ];
                        }
                    }
                }

                newChannels[i] = new FilteredImageChannel(size, newValues);
            }

            Output = new FilteredImage(image.NumberOfChannels, newChannels);
        }
        public override void ComputeOutput()
        {
            FilteredImageChannel[] channels = new FilteredImageChannel[FilterNumber];
            FilteredImage          img      = (FilteredImage)PreviousLayer.GetData();

            bool samePadding = (Padding == "same") ? true : false;


            Task[] tasks = new Task[FilterNumber];

            for (int i = 0; i < FilterNumber; i++)
            {
                int taski = 0 + i;

                tasks[taski] = Task.Run(() =>
                {
                    channels[taski] = Filters[taski].Convolve(img, samePadding);
                });
            }

            Task.WaitAll(tasks);



            //for (int i = 0; i < FilterNumber; i++)
            //{
            //    channels[i] = Filters[i].Convolve(img, samePadding);
            //}

            OutputBeforeActivation = new FilteredImage(FilterNumber, channels);
            OutputImage            = ActivationFunction.Activate(OutputBeforeActivation);
        }
        public override LayerOutput[] Backpropagate(LayerOutput[] nextOutput, double learningRate)
        {
            FilteredImage image       = (FilteredImage)PreviousLayer.GetData();
            int           outputIndex = 0;

            FilteredImageChannel[] channels = new FilteredImageChannel[image.NumberOfChannels];

            for (int channel = 0; channel < image.NumberOfChannels; channel++)
            {
                double[,] values = new double[image.Size, image.Size];
                for (int valuesI = 0; valuesI < image.Size; valuesI++)
                {
                    for (int valuesJ = 0; valuesJ < image.Size; valuesJ++)
                    {
                        values[valuesI, valuesJ] = nextOutput[outputIndex].Sum();
                        outputIndex++;
                    }
                }
                channels[channel] = new FilteredImageChannel(image.Size, values);
            }

            return(new FilteredImage[1] {
                new FilteredImage(image.NumberOfChannels, channels)
            });
        }
        public override void ComputeOutput()
        {
            FilteredImage image       = (FilteredImage)PreviousLayer.GetData();
            int           outputIndex = 0;

            //for(int valuesI = 0; valuesI < image.Size; valuesI++)
            //     {
            //     for (int valuesJ = 0; valuesJ < image.Size; valuesJ++)
            //     {
            //         for (int channel = 0; channel < image.NumberOfChannels; channel++)
            //         {

            //             Output.Values[outputIndex] = image.Channels[channel].Values[valuesI, valuesJ];
            //             outputIndex++;
            //         }
            //     }
            //     }

            for (int channel = 0; channel < image.NumberOfChannels; channel++)
            {
                for (int valuesI = 0; valuesI < image.Size; valuesI++)
                {
                    for (int valuesJ = 0; valuesJ < image.Size; valuesJ++)
                    {
                        Output.Values[outputIndex] = image.Channels[channel].Values[valuesI, valuesJ];
                        outputIndex++;
                    }
                }
            }
        }
        public override LayerOutput[] Backpropagate(LayerOutput[] nextOutput, double learningRate)
        {
            FilteredImage input = (FilteredImage)PreviousLayer.GetData();

            FilteredImageChannel[] outputChannels = new FilteredImageChannel[input.NumberOfChannels];

            FilteredImage nextErrors = (FilteredImage)nextOutput[0];

            int errorSize  = nextErrors.Size;
            int outputSize = input.Size;

            for (int i = 0; i < input.NumberOfChannels; i++)
            {
                outputChannels[i] = new FilteredImageChannel(outputSize);
            }

            Task[] tasks = new Task[input.NumberOfChannels];

            for (int channel = 0; channel < input.NumberOfChannels; channel++)
            {
                int taskc = 0 + channel;

                tasks[taskc] = Task.Run(() =>
                {
                    for (int channelI = 0; channelI + Pool <= outputSize; channelI += Pool)
                    {
                        for (int channelJ = 0; channelJ + Pool <= outputSize; channelJ += Pool)
                        {
                            int maxi    = 0, maxj = 0;
                            double maxx = input.Channels[taskc].Values[channelI, channelJ];

                            for (int poolI = 0; poolI < Pool; poolI++)
                            {
                                for (int poolJ = 0; poolJ < Pool; poolJ++)
                                {
                                    int i = channelI + poolI, j = channelJ + poolJ;
                                    if (input.Channels[taskc].Values[i, j] > maxx)
                                    {
                                        maxx = input.Channels[taskc].Values[i, j];
                                        maxi = i;
                                        maxj = j;
                                    }
                                }
                            }

                            outputChannels[taskc].Values[maxi, maxj] =
                                nextErrors.Channels[taskc].Values[channelI / Pool, channelJ / Pool];
                        }
                    }
                });
            }

            Task.WaitAll(tasks);

            return(new FilteredImage[1] {
                new FilteredImage(input.NumberOfChannels, outputChannels)
            });
        }
        public override LayerOutput[] Backpropagate(LayerOutput[] nextOutput, double learningRate)
        {
            FilteredImage[] newErrors = new FilteredImage[FilterNumber];

            FilteredImage previous = (FilteredImage)PreviousLayer.GetData();

            FilteredImage nextErrors = (FilteredImage)nextOutput[0];

            FilteredImage activationDerivatives = ActivationFunction.GetDerivative(OutputBeforeActivation);

            for (int i = 0; i < nextErrors.NumberOfChannels; i++)
            {
                for (int j = 0; j < nextErrors.Size; j++)
                {
                    for (int k = 0; k < nextErrors.Size; k++)
                    {
                        nextErrors.Channels[i].Values[j, k] *= activationDerivatives.Channels[i].Values[j, k];
                    }
                }
            }

            bool samePadding = (Padding == "same") ? true : false;

            Task[] tasks = new Task[FilterNumber];

            for (int i = 0; i < FilterNumber; i++)
            {
                int taski = 0 + i;

                tasks[taski] = Task.Run(() =>
                {
                    newErrors[taski] = Filters[taski].Backpropagate(previous,
                                                                    nextErrors.Channels[taski], learningRate, samePadding);
                });
            }

            Task.WaitAll(tasks);


            //for (int i = 0; i < FilterNumber; i++)
            //{
            //    newErrors[i] = Filters[i].Backpropagate(previous,
            //            nextErrors.Channels[i], learningRate, samePadding);
            //}



            //for (int i = 0; i < FilterNumber; i++)
            //{
            //    newErrors[i] = Filters[i].Backpropagate(previous,
            //        activationDerivatives.Channels[i], learningRate, samePadding);
            //}

            return(newErrors);
        }
        public override void ComputeOutput()
        {
            FilteredImage input = (FilteredImage)PreviousLayer.GetData();

            FilteredImageChannel[] outputChannels = new FilteredImageChannel[input.NumberOfChannels];
            int inputSize  = input.Size;
            int outputSize = inputSize / Pool;

            Task[] tasks = new Task[input.NumberOfChannels];

            for (int channel = 0; channel < input.NumberOfChannels; channel++)
            {
                int taskc = 0 + channel;

                tasks[taskc] = Task.Run(() =>
                {
                    FilteredImageChannel auxInput = input.Channels[taskc];

                    double[,] outputValues = new double[outputSize, outputSize];

                    for (int channelI = 0; channelI + Pool <= inputSize; channelI += Pool)
                    {
                        for (int channelJ = 0; channelJ + Pool <= inputSize; channelJ += Pool)
                        {
                            double maxx = auxInput.Values[channelI, channelJ];

                            for (int poolI = 0; poolI < Pool; poolI++)
                            {
                                for (int poolJ = 0; poolJ < Pool; poolJ++)
                                {
                                    if (maxx < auxInput.Values[channelI + poolI, channelJ + poolJ])
                                    {
                                        maxx = auxInput.Values[channelI + poolI, channelJ + poolJ];
                                    }
                                }
                            }

                            outputValues[channelI / Pool, channelJ / Pool] = maxx;
                        }
                    }
                    outputChannels[taskc] = new FilteredImageChannel(outputSize, outputValues);
                });
            }

            Task.WaitAll(tasks);

            Output = new FilteredImage(input.NumberOfChannels, outputChannels);
        }
 private void InitializeOutput()
 {
     if (Output == null)
     {
         LayerOutput previousData = PreviousLayer.GetData();
         if (previousData is FlattenedImage)
         {
             FlattenedImage previous = (FlattenedImage)previousData;
             Output = new FlattenedImage(previous.Size);
         }
         else
         {
             FilteredImage previous = (FilteredImage)previousData;
             Output = new FilteredImage(previous.NumberOfChannels, previous.Size);
         }
     }
 }
Beispiel #9
0
        public override void ComputeOutput()
        {
            FlattenedImage previous = (FlattenedImage)PreviousLayer.GetData();

            Task[] tasks = new Task[NumberOfUnits];

            for (int i = 0; i < NumberOfUnits; i++)
            {
                int taski = 0 + i;

                tasks[taski] = Task.Run(() =>
                {
                    Output.Values[taski] = Units[taski].ComputeOutput(previous);
                });
            }

            Task.WaitAll(tasks);

            Output = ActivationFunction.Activate(Output);
        }
        private void ComputeFlattenedImage()
        {
            FlattenedImage previous = (FlattenedImage)PreviousLayer.GetData();

            double[] newValues = new double[previous.Size];

            for (int i = 0; i < previous.Size; i++)
            {
                if (GlobalRandom.GetRandomDouble() < Rate)
                {
                    newValues[i] = 0;
                }
                else
                {
                    newValues[i] = previous.Values[i];
                }
            }

            Output = new FlattenedImage(previous.Size, newValues);
        }
Beispiel #11
0
        public override LayerOutput[] Backpropagate(LayerOutput[] nextOutput, double learningRate)
        {
            int weightsPerUnit = Units[0].NumberOfWeights;

            FlattenedImage[] result   = new FlattenedImage[weightsPerUnit];
            FlattenedImage   previous = (FlattenedImage)PreviousLayer.GetData();

            for (int i = 0; i < weightsPerUnit; i++)
            {
                result[i] = new FlattenedImage(NumberOfUnits);
            }

            FlattenedImage activationDerivative = ActivationFunction.GetDerivative(Output);

            Task[] tasks = new Task[NumberOfUnits];

            for (int unit = 0; unit < NumberOfUnits; unit++)
            {
                int tasku = 0 + unit;

                tasks[tasku] = Task.Run(() =>
                {
                    Unit unitAux = Units[tasku];

                    FlattenedImage nextErrors = (FlattenedImage)nextOutput[tasku];

                    double unitSum        = nextErrors.Values.Sum();
                    double unitDerivative = unitSum * activationDerivative.Values[tasku];

                    for (int weight = 0; weight < unitAux.NumberOfWeights; weight++)
                    {
                        Monitor.Enter(result);
                        result[weight].Values[tasku] = unitDerivative * unitAux.Weights[weight];
                        Monitor.Exit(result);
                        double deltaW            = unitDerivative * previous.Values[weight];
                        unitAux.Weights[weight] -= learningRate * deltaW;
                    }
                });
            }

            Task.WaitAll(tasks);

            //for (int unit = 0; unit < NumberOfUnits; unit++)
            //{
            //    Unit unitAux = Units[unit];

            //        FlattenedImage nextErrors = (FlattenedImage)nextOutput[unit];

            //        double unitSum = nextErrors.Sum();
            //        double unitDerivative = unitSum * activationDerivative.Values[unit];

            //        for (int weight = 0; weight < unitAux.NumberOfWeights; weight++)
            //        {
            //            Monitor.Enter(result);
            //            result[weight].Values[unit] = unitDerivative * unitAux.Weights[weight];
            //            Monitor.Exit(result);
            //            double deltaW = unitDerivative * previous.Values[weight];
            //            unitAux.Weights[weight] -= learningRate * deltaW;
            //        }
            //}

            return(result);
        }