OutputVector is a sparse vector class, used for representing classifier outputs.
示例#1
0
        public static OutputVector operator /(OutputVector outvector, float val)
        {
            OutputVector res = outvector;

            res._values = res._values / val;
            return(res);
        }
示例#2
0
        public int Classify(Floatarray v)
        {
            OutputVector p = new OutputVector();

            XOutputs(p, v);
            return(p.ArgMax());
        }
示例#3
0
        protected override float Outputs(OutputVector result, Floatarray v)
        {
            result.Clear();
            charclass.Object.XOutputs(result, v);
            CHECK_ARG(result.nKeys() > 0, "result.nKeys() > 0");

            if (PGetb("junk") && !DisableJunk && !junkclass.IsEmpty)
            {
                result.Normalize();
                OutputVector jv = new OutputVector();
                junkclass.Object.XOutputs(jv, v);
                for (int i = 0; i < result.nKeys(); i++)
                {
                    result.Values[i] *= jv.Value(0);
                }
                result[jc()] = jv.Value(1);
            }

            if (PGeti("ul") > 0 && !ulclass.IsEmpty)
            {
                throw new Exception("ulclass not implemented");
            }

            return(0.0f);
        }
示例#4
0
        public float Outputs(Floatarray p, Floatarray x)
        {
            OutputVector ov   = new OutputVector();
            float        cost = XOutputs(ov, x);

            p.Clear();
            p.Copy(ov.AsArray());
            return(cost);
        }
示例#5
0
 protected override float Outputs(OutputVector result, Floatarray v)
 {
     Floatarray outar = new Floatarray();
     float cost = OutputsDense(outar, v);
     result.Clear();
     for (int i = 0; i < outar.Length(); i++)
         result[i2c[i]] = outar[i];
     return cost;
 }
示例#6
0
        protected override float Outputs(OutputVector result, Floatarray v)
        {
            Floatarray outar = new Floatarray();
            float      cost  = OutputsDense(outar, v);

            result.Clear();
            for (int i = 0; i < outar.Length(); i++)
            {
                result[i2c[i]] = outar[i];
            }
            return(cost);
        }
示例#7
0
 /// <summary>
 /// Run recognize
 /// </summary>
 /// <param name="ov">network output</param>
 /// <param name="v">values can be 0..1 or 0..255, see RequireUByteInput property</param>
 /// <returns></returns>
 public float XOutputs(OutputVector ov, Floatarray v)
 {
     if (!_extractor.IsEmpty)
     {
         Floatarray temp = new Floatarray();
         _extractor.Object.Extract(temp, v);
         return(Outputs(ov, temp));
     }
     else
     {
         return(Outputs(ov, v));
     }
 }
示例#8
0
        protected override float Outputs(OutputVector result, Floatarray v)
        {
            result.Clear();
            charclass.Object.XOutputs(result, v);
            CHECK_ARG(result.nKeys() > 0, "result.nKeys() > 0");

            if (PGetb("junk") && !DisableJunk && !junkclass.IsEmpty)
            {
                result.Normalize();
                OutputVector jv = new OutputVector();
                junkclass.Object.XOutputs(jv, v);
                for (int i = 0; i < result.nKeys(); i++)
                    result.Values[i] *= jv.Value(0);
                result[jc()] = jv.Value(1);
            }

            if (PGeti("ul") > 0 && !ulclass.IsEmpty)
            {
                throw new Exception("ulclass not implemented");
            }

            return 0.0f;
        }
示例#9
0
文件: Linerec.cs 项目: nickun/OCRonet
        /// <summary>
        /// This is a weird, optional method that exposes character segmentation
        /// for those line recognizers that have it segmentation contains colored pixels,
        /// and a transition in the transducer of the form * --- 1/eps --> * --- 2/a --> *
        /// means that pixels with color 1 and 2 together form the letter "a"
        /// </summary>
        public override double RecognizeLine(Intarray segmentation_, IGenericFst result, Bytearray image_)
        {
            double rate = 0.0;
            CHECK_ARG(image_.Dim(1) < PGeti("maxheight"),
                String.Format("input line too high ({0} x {1})", image_.Dim(0), image_.Dim(1)));
            CHECK_ARG(image_.Dim(1) * 1.0 / image_.Dim(0) < PGetf("maxaspect"),
                String.Format("input line has bad aspect ratio ({0} x {1})", image_.Dim(0), image_.Dim(1)));
            bool use_reject = PGetb("use_reject") && !DisableJunk;
            //Console.WriteLine("IMG: imin:{0} imax:{1}", NarrayUtil.ArgMin(image_), NarrayUtil.ArgMax(image_));
            Bytearray image = new Bytearray();
            image.Copy(image_);

            SetLine(image_);

            if (PGeti("invert") > 0)
                NarrayUtil.Sub(NarrayUtil.Max(image), image);
            segmentation_.Copy(segmentation);
            Bytearray available = new Bytearray();
            Floatarray cp = new Floatarray();
            Floatarray ccosts = new Floatarray();
            Floatarray props = new Floatarray();
            OutputVector p = new OutputVector();
            int ncomponents = grouper.Object.Length();
            int minclass = PGeti("minclass");
            float minprob = PGetf("minprob");
            float space_yes = PGetf("space_yes");
            float space_no = PGetf("space_no");
            float maxcost = PGetf("maxcost");

            // compute priors if possible; fall back on
            // using no priors if no counts are available
            Floatarray priors = new Floatarray();
            bool use_priors = PGeti("use_priors") > 0;
            if (use_priors)
            {
                if (counts.Length() > 0)
                {
                    priors.Copy(counts);
                    priors /= NarrayUtil.Sum(priors);
                }
                else
                {
                    if (!counts_warned)
                        Global.Debugf("warn", "use_priors specified but priors unavailable (old model)");
                    use_priors = false;
                    counts_warned = true;
                }
            }

            EstimateSpaceSize();

            for (int i = 0; i < ncomponents; i++)
            {
                Rect b;
                Bytearray mask = new Bytearray();
                grouper.Object.GetMask(out b, ref mask, i, 0);
                Bytearray cv = new Bytearray();
                grouper.Object.ExtractWithMask(cv, mask, image, i, 0);
                //ImgIo.write_image_gray("extrmask_image.png", cv);
                Floatarray v = new Floatarray();
                v.Copy(cv);
                v /= 255.0f;
                float ccost = classifier.Object.XOutputs(p, v);
                if (use_reject && classifier.Object.HigherOutputIsBetter)
                {
                    ccost = 0;
                    float total = p.Sum();
                    if (total > 1e-11f)
                    {
                        //p /= total;
                    }
                    else
                        p.Values.Fill(0.0f);
                }
                int count = 0;

                Global.Debugf("dcost", "output {0}", p.Keys.Length());
                for (int index = 0; index < p.Keys.Length(); index++)
                {
                    int j = p.Keys[index];
                    if (j < minclass) continue;
                    if (j == reject_class) continue;
                    float value = p.Values[index];
                    if (value <= 0.0f) continue;
                    if (value < minprob) continue;
                    float pcost = classifier.Object.HigherOutputIsBetter ? (float)-Math.Log(value) : value;
                    Global.Debugf("dcost", "{0} {1} {2}", j, pcost + ccost, (j > 32 ? (char)j : '_'));
                    float total_cost = pcost + ccost;
                    if (total_cost < maxcost)
                    {
                        if (use_priors)
                        {
                            total_cost -= (float)-Math.Log(priors[j]);
                        }
                        grouper.Object.SetClass(i, j, total_cost);
                        count++;
                    }
                }
                Global.Debugf("dcost", "");

                if (count == 0)
                {
                    float xheight = 10.0f;
                    if (b.Height() < xheight / 2 && b.Width() < xheight / 2)
                    {
                        grouper.Object.SetClass(i, (int)'~', high_cost / 2);
                    }
                    else
                    {
                        grouper.Object.SetClass(i, (int)'#', (b.Width() / xheight) * high_cost);
                    }
                }
                if (grouper.Object.PixelSpace(i) > space_threshold)
                {
                    Global.Debugf("spaces", "space {0}", grouper.Object.PixelSpace(i));
                    grouper.Object.SetSpaceCost(i, space_yes, space_no);
                }
            }

            grouper.Object.GetLattice(result);
            return rate;
        }
示例#10
0
 protected virtual float Outputs(OutputVector ov, Floatarray temp)
 {
     throw new NotImplementedException();
 }
示例#11
0
文件: IModel.cs 项目: nickun/OCRonet
 public int Classify(Floatarray v)
 {
     OutputVector p = new OutputVector();
     XOutputs(p, v);
     return p.ArgMax();
 }
示例#12
0
文件: IModel.cs 项目: nickun/OCRonet
 protected virtual float Outputs(OutputVector ov, Floatarray temp)
 {
     throw new NotImplementedException();
 }
示例#13
0
文件: IModel.cs 项目: nickun/OCRonet
 /// <summary>
 /// Run recognize
 /// </summary>
 /// <param name="ov">network output</param>
 /// <param name="v">values can be 0..1 or 0..255, see RequireUByteInput property</param>
 /// <returns></returns>
 public float XOutputs(OutputVector ov, Floatarray v)
 {
     if (!_extractor.IsEmpty)
     {
         Floatarray temp = new Floatarray();
         _extractor.Object.Extract(temp, v);
         return Outputs(ov, temp);
     }
     else
         return Outputs(ov, v);
 }
示例#14
0
文件: IModel.cs 项目: nickun/OCRonet
 public float Outputs(Floatarray p, Floatarray x)
 {
     OutputVector ov = new OutputVector();
     float cost = XOutputs(ov, x);
     p.Clear();
     p.Copy(ov.AsArray());
     return cost;
 }
示例#15
0
        /// <summary>
        /// This is a weird, optional method that exposes character segmentation
        /// for those line recognizers that have it segmentation contains colored pixels,
        /// and a transition in the transducer of the form * --- 1/eps --> * --- 2/a --> *
        /// means that pixels with color 1 and 2 together form the letter "a"
        /// </summary>
        public override double RecognizeLine(Intarray segmentation_, IGenericFst result, Bytearray image_)
        {
            double rate = 0.0;

            CHECK_ARG(image_.Dim(1) < PGeti("maxheight"),
                      String.Format("input line too high ({0} x {1})", image_.Dim(0), image_.Dim(1)));
            CHECK_ARG(image_.Dim(1) * 1.0 / image_.Dim(0) < PGetf("maxaspect"),
                      String.Format("input line has bad aspect ratio ({0} x {1})", image_.Dim(0), image_.Dim(1)));
            bool use_reject = PGetb("use_reject") && !DisableJunk;
            //Console.WriteLine("IMG: imin:{0} imax:{1}", NarrayUtil.ArgMin(image_), NarrayUtil.ArgMax(image_));
            Bytearray image = new Bytearray();

            image.Copy(image_);

            SetLine(image_);

            if (PGeti("invert") > 0)
            {
                NarrayUtil.Sub(NarrayUtil.Max(image), image);
            }
            segmentation_.Copy(segmentation);
            Bytearray    available   = new Bytearray();
            Floatarray   cp          = new Floatarray();
            Floatarray   ccosts      = new Floatarray();
            Floatarray   props       = new Floatarray();
            OutputVector p           = new OutputVector();
            int          ncomponents = grouper.Object.Length();
            int          minclass    = PGeti("minclass");
            float        minprob     = PGetf("minprob");
            float        space_yes   = PGetf("space_yes");
            float        space_no    = PGetf("space_no");
            float        maxcost     = PGetf("maxcost");

            // compute priors if possible; fall back on
            // using no priors if no counts are available
            Floatarray priors     = new Floatarray();
            bool       use_priors = PGeti("use_priors") > 0;

            if (use_priors)
            {
                if (counts.Length() > 0)
                {
                    priors.Copy(counts);
                    priors /= NarrayUtil.Sum(priors);
                }
                else
                {
                    if (!counts_warned)
                    {
                        Global.Debugf("warn", "use_priors specified but priors unavailable (old model)");
                    }
                    use_priors    = false;
                    counts_warned = true;
                }
            }

            EstimateSpaceSize();

            for (int i = 0; i < ncomponents; i++)
            {
                Rect      b;
                Bytearray mask = new Bytearray();
                grouper.Object.GetMask(out b, ref mask, i, 0);
                Bytearray cv = new Bytearray();
                grouper.Object.ExtractWithMask(cv, mask, image, i, 0);
                //ImgIo.write_image_gray("extrmask_image.png", cv);
                Floatarray v = new Floatarray();
                v.Copy(cv);
                v /= 255.0f;
                float ccost = classifier.Object.XOutputs(p, v);
                if (use_reject && classifier.Object.HigherOutputIsBetter)
                {
                    ccost = 0;
                    float total = p.Sum();
                    if (total > 1e-11f)
                    {
                        //p /= total;
                    }
                    else
                    {
                        p.Values.Fill(0.0f);
                    }
                }
                int count = 0;

                Global.Debugf("dcost", "output {0}", p.Keys.Length());
                for (int index = 0; index < p.Keys.Length(); index++)
                {
                    int j = p.Keys[index];
                    if (j < minclass)
                    {
                        continue;
                    }
                    if (j == reject_class)
                    {
                        continue;
                    }
                    float value = p.Values[index];
                    if (value <= 0.0f)
                    {
                        continue;
                    }
                    if (value < minprob)
                    {
                        continue;
                    }
                    float pcost = classifier.Object.HigherOutputIsBetter ? (float)-Math.Log(value) : value;
                    Global.Debugf("dcost", "{0} {1} {2}", j, pcost + ccost, (j > 32 ? (char)j : '_'));
                    float total_cost = pcost + ccost;
                    if (total_cost < maxcost)
                    {
                        if (use_priors)
                        {
                            total_cost -= (float)-Math.Log(priors[j]);
                        }
                        grouper.Object.SetClass(i, j, total_cost);
                        count++;
                    }
                }
                Global.Debugf("dcost", "");

                if (count == 0)
                {
                    float xheight = 10.0f;
                    if (b.Height() < xheight / 2 && b.Width() < xheight / 2)
                    {
                        grouper.Object.SetClass(i, (int)'~', high_cost / 2);
                    }
                    else
                    {
                        grouper.Object.SetClass(i, (int)'#', (b.Width() / xheight) * high_cost);
                    }
                }
                if (grouper.Object.PixelSpace(i) > space_threshold)
                {
                    Global.Debugf("spaces", "space {0}", grouper.Object.PixelSpace(i));
                    grouper.Object.SetSpaceCost(i, space_yes, space_no);
                }
            }

            grouper.Object.GetLattice(result);
            return(rate);
        }
示例#16
0
 private void DoTestRecognize(LenetClassifier classifier)
 {
     OutputVector ov = new OutputVector();
     Floatarray v = new Floatarray();
     Bytearray ba = new Bytearray(1, 1);
     ImgIo.read_image_gray(ba, testPngFileName);
     NarrayUtil.Sub(255, ba);
     v.Copy(ba);
     v /= 255.0;
     classifier.XOutputs(ov, v);
     Console.WriteLine("Featured output class '{0}', score '{1}'", (char)ov.Key(ov.BestIndex), ov.Value(ov.BestIndex));
 }