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 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 void CompileLayer(NetworkLayer previousLayer)
        {
            PreviousLayer = previousLayer;
            FilteredImage previous = (FilteredImage)previousLayer.GetData();

            Output = new FilteredImage(previous.NumberOfChannels, previous.Size / Pool);
        }
        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 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 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 FilteredImageChannel Convolve(FilteredImage input, bool samePadding)
        {
            int resultSize;

            if (samePadding)
            {
                resultSize = input.Size;
            }
            else
            {
                resultSize = input.Size - KernelSize + 1;
            }

            double[,] values = new double[resultSize, resultSize];

            double[,] kernelOutput;


            Task[] tasks = new Task[KernelNumber];

            for (int i = 0; i < input.NumberOfChannels; i++)
            {
                int taski = 0 + i;

                tasks[taski] = Task.Run(() =>
                {
                    kernelOutput = Kernels[taski].Convolve(input.Channels[taski], samePadding);

                    for (int outputI = 0; outputI < resultSize; outputI++)
                    {
                        for (int outputJ = 0; outputJ < resultSize; outputJ++)
                        {
                            Monitor.Enter(values);
                            values[outputI, outputJ] += kernelOutput[outputI, outputJ];
                            Monitor.Exit(values);
                        }
                    }
                });
            }

            Task.WaitAll(tasks);


            //for (int i = 0; i < input.NumberOfChannels; i++)
            //{
            //    kernelOutput = Kernels[i].Convolve(input.Channels[i], samePadding);

            //    for (int outputI = 0; outputI < resultSize; outputI++)
            //    {
            //        for (int outputJ = 0; outputJ < resultSize; outputJ++)
            //        {
            //            values[outputI, outputJ] += kernelOutput[outputI, outputJ];
            //        }
            //    }
            //}

            return(new FilteredImageChannel(resultSize, values));
        }
 public override void CompileLayer(NetworkLayer previousLayer)
 {
     PreviousLayer = previousLayer;
     if (Output == null)
     {
         FilteredImage previous = (FilteredImage)previousLayer.GetData();
         Output = new FlattenedImage(previous.Size * previous.Size * previous.NumberOfChannels);
     }
 }
        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);
        }
Beispiel #10
0
        public double[] RecogniseImage(FilteredImage image)
        {
            Input.SetInputImage(image);

            foreach (NetworkLayer layer in NetworkLayers)
            {
                layer.ComputeOutput();
            }

            return(GetOutput());
        }
        public FilteredImage Backpropagate(FilteredImage previous, FilteredImageChannel nextErrors, double learningRate, bool samePadding)
        {
            FilteredImageChannel[] newChannels = new FilteredImageChannel[KernelNumber];

            for (int k = 0; k < KernelNumber; k++)
            {
                newChannels[k] = Kernels[k].Backpropagate(previous.Channels[k], nextErrors, KernelNumber, learningRate, samePadding);
            }

            return(new FilteredImage(KernelNumber, newChannels));
        }
Beispiel #12
0
 public override void CompileLayer(NetworkLayer previousLayer)
 {
     if (Output == null)
     {
         if (ColorScheme == "rgb")
         {
             Output = new FilteredImage(3, Size);
         }
         else
         {
             Output = new FilteredImage(1, Size);
         }
     }
 }
        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);
         }
     }
 }
        public override void CompileLayer(NetworkLayer previousLayer)
        {
            PreviousLayer = previousLayer;
            FilteredImage previous = (FilteredImage)PreviousLayer.GetData();

            if (Filters[0] == null)
            {
                CreateKernels(previous.NumberOfChannels);
            }

            if (Padding == "same")
            {
                OutputImage = new FilteredImage(FilterNumber, previous.Size);
            }
            else
            {
                OutputImage = new FilteredImage(FilterNumber, previous.Size - FilterSize + 1);
            }
        }
Beispiel #16
0
        public override FilteredImage GetDerivative(FilteredImage output)
        {
            FilteredImageChannel[] resultChannels = new FilteredImageChannel[output.NumberOfChannels];
            double[,] resultValues;

            for (int c = 0; c < output.NumberOfChannels; c++)
            {
                resultValues = new double[output.Size, output.Size];

                for (int i = 0; i < output.Size; i++)
                {
                    for (int j = 0; j < output.Size; j++)
                    {
                        resultValues[i, j] = GetValueDerivative(output.Channels[c].Values[i, j]);
                    }
                }

                resultChannels[c] = new FilteredImageChannel(output.Size, resultValues);
            }

            return(new FilteredImage(output.NumberOfChannels, resultChannels));
        }
Beispiel #17
0
        public override FilteredImage Activate(FilteredImage img)
        {
            FilteredImageChannel[] resultChannels = new FilteredImageChannel[img.NumberOfChannels];
            double[,] resultValues;

            for (int c = 0; c < img.NumberOfChannels; c++)
            {
                resultValues = new double[img.Size, img.Size];

                for (int i = 0; i < img.Size; i++)
                {
                    for (int j = 0; j < img.Size; j++)
                    {
                        resultValues[i, j] = ActivateValue(img.Channels[c].Values[i, j]);
                    }
                }

                resultChannels[c] = new FilteredImageChannel(img.Size, resultValues);
            }

            return(new FilteredImage(img.NumberOfChannels, resultChannels));
        }
Beispiel #18
0
 public virtual FilteredImage Activate(FilteredImage img)
 {
     throw new Exception();
 }
Beispiel #19
0
 public virtual FilteredImage GetDerivative(FilteredImage output)
 {
     throw new Exception();
 }
Beispiel #20
0
 public void SetInputImage(FilteredImage image)
 {
     Output = image;
 }