public NoiseRemovalComputation(NeuralComputation comp)
        {
            Contract.Requires(comp != null);

            BaseComputation = comp;
        }
Esempio n. 2
0
 private static void Save(NeuralNetwork network, IFeaturedInputOutput featuredObject)
 {
     Console.WriteLine("Saving ...");
     var comp = new NeuralComputation(trainedNet, featuredObject: featuredObject);
     string fn = string.Format("comp {0}-{1}_{2}-{3}-{4}.xml", DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
     using (var xml = XmlWriter.Create(fn, new XmlWriterSettings { Indent = true }))
     {
         var s = new DataContractSerializer(typeof(NeuralComputation));
         s.WriteObject(xml, comp);
         xml.Flush();
     }
 }
Esempio n. 3
0
        private static void RemoveNoise(NeuralComputation computation, string path, double noiseLevel)
        {
            var comps = Enumerable.Range(0, 15).Select(idx => new NoiseRemovalComputation(computation.Clone())).ToList();
            comps.Add(new NoiseRemovalComputation(computation));
            int compIdx = 0;
            
            ImageBuffer source, dest;
            using (var sourceBmp = new Bitmap(path))
            {
                source = new ImageBuffer(sourceBmp);
                AddNoise(source, noiseLevel);
                dest = new ImageBuffer(source.Width, source.Height);
            }

            int size = 7, blockSize = 16;
            int w = source.Width, h = source.Height, soFar = 0, msTime = 0;
            SpinLock infoLock = new SpinLock(), compLock = new SpinLock();

            Parallel.For(0, h / blockSize + 1, (idx, state) =>
            {
                NoiseRemovalComputation comp;
                bool compTaken = false;
                try
                {
                    compLock.Enter(ref compTaken);
                    comp = comps[compIdx++ % comps.Count];
                }
                finally
                {
                    if (compTaken) compLock.Exit();
                }

                //Console.WriteLine(compIdx);

                int pos = idx * blockSize;
                var rBytes = new byte[size * size];
                var bBytes = new byte[size * size];
                var gBytes = new byte[size * size];
                Color[] sourceColors = new Color[size * size];
                for (int y = pos; y < pos + blockSize && y < h; y++)
                {
                    var sw = new Stopwatch();
                    for (int x = 0; x < w; x++)
                    {
                        ReadBytes(source, rBytes, gBytes, bBytes, size, x, y);

                        sw.Start();
                        byte destByteR = comp.Compute(rBytes, noiseLevel);
                        byte destByteG = comp.Compute(gBytes, noiseLevel);
                        byte destByteB = comp.Compute(bBytes, noiseLevel);
                        sw.Stop();

                        WriteByte(dest, x, y, destByteR, destByteG, destByteB);
                    }

                    bool infoTaken = false;
                    try
                    {
                        infoLock.Enter(ref infoTaken);
                        msTime += (int)sw.ElapsedMilliseconds;
                        Console.WriteLine("Processing line: {0} / {1}", ++soFar, h);
                    }
                    finally
                    {
                        if (infoTaken) infoLock.Exit();
                    }
                }
            });

            Console.WriteLine("Comp: {0}ms", msTime);

            dest.Save("result.png", ImageFormat.Png);  
        }