示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cs"></param>
        public ActorScriptSection(CustomSection cs)
        {
            var stream           = new MemoryStream((byte[])cs.Content);
            var reader           = new Reader(stream);
            var previousSection  = ActorScriptSubsection.Labels;
            var preSectionOffset = reader.Offset;

            while (reader.TryReadVarUInt7(out var id)) //At points where TryRead is used, the stream can safely end.
            {
                if (id != 0 && (ActorScriptSubsection)id < previousSection)
                {
                    throw new ModuleLoadException($"Sections out of order; section {(ActorScriptSubsection)id} encounterd after {previousSection}.", preSectionOffset);
                }
                var payloadLength = reader.ReadVarUInt32();

                switch ((ActorScriptSubsection)id)
                {
                case ActorScriptSubsection.Labels:
                {
                    var count = reader.ReadVarUInt32();
                    Labels = new LabelMap((int)count);
                    for (int i = 0; i < count; i++)
                    {
                        var index      = reader.ReadVarUInt32();
                        var nameLength = reader.ReadVarUInt32();
                        var name       = reader.ReadString(nameLength);
                        Labels.Add(index, name);
                    }
                }
                break;
                }

                previousSection = (ActorScriptSubsection)id;
            }
        }
示例#2
0
        public void SetUp()
        {
            _axisMap  = new FakeAxisMap();
            _colorMap = new FakeColorMap();
            _sizeMap  = new FakeSizeMap();
            _labelMap = new FakeLabelMap();

            _column       = new ColumnBuilder().Build();
            _colorPalette = new ColorPaletteBuilder().Build();

            _mockAxisMapFactory = new Mock <IAxisMapFactory>();
            _mockAxisMapFactory.Setup(p => p.Create(_column, 0d, 1d, SortOrder.Ascending))
            .Returns(_axisMap);

            _mockColorMapFactory = new Mock <IColorMapFactory>();
            _mockColorMapFactory.Setup(p => p.Create(_column, _colorPalette, SortOrder.Ascending))
            .Returns(_colorMap);

            _mockSizeMapFactory = new Mock <ISizeMapFactory>();
            _mockSizeMapFactory.Setup(p => p.Create(_column, 0d, 1d, SortOrder.Ascending))
            .Returns(_sizeMap);

            _mockLabelMapFactory = new Mock <ILabelMapFactory>();
            _mockLabelMapFactory.Setup(p => p.Create(_column))
            .Returns(_labelMap);

            _factory = new MapFactory(
                _mockAxisMapFactory.Object,
                _mockColorMapFactory.Object,
                _mockSizeMapFactory.Object,
                _mockLabelMapFactory.Object);
        }
示例#3
0
        public void Train()
        {
            videoGrab.Pause();
            var images = trainDataDAL.GetImages().ToList();

            labelMap = new LabelMap(trainDataDAL.GetLabelMap());
            var faceEmbeddings = images
                                 .Select(img => (labelMap.Map[img.Label], detectionModule.GetFaceEmbedding(img.Image)))
                                 .Where(tuple => tuple.Item2 != null)
                                 .ToList();

            if (faceEmbeddings != null && faceEmbeddings.Any())
            {
                recognitionModule.Train(faceEmbeddings, trainedModel, "PersonalModels//First//");
            }

            var faceEmbeddingsSecond = images
                                       .Select(img => (labelMap.Map[img.Label], detectionModule.GetFaceEmbeddingSecond(img.Image)))
                                       .Where(tuple => tuple.Item2 != null)
                                       .ToList();

            if (faceEmbeddings != null && faceEmbeddings.Any())
            {
                recognitionModule.Train(faceEmbeddingsSecond, trainedSecondModel, "PersonalModels//Second//");
            }
            _hasTrainedModel = false; //force reloading the new model before start recognition
            videoGrab.Start();
        }
示例#4
0
        private LabelMap loadLabelMap()
        {
            string   strFile = getDataFile(dataset_name, "labelmap_voc.prototxt");
            RawProto proto   = RawProtoFile.LoadFromFile(strFile);

            return(LabelMap.FromProto(proto));
        }
示例#5
0
 public async Task Setup()
 {
     _cpuDebug     = new MockCpuDebug(new MockCpuHoldEvent(), new MockCpuStepEvent());
     _addressMap   = new MockMemoryDebug();
     _labels       = new LabelMap();
     _logFormatter = new DebugLogFormatter(_labels);
     await Task.Delay(0);
 }
示例#6
0
 /// <summary>
 /// Constructor for the <c>Scanner</c> object. This is used
 /// to scan the provided class for annotations that are used to
 /// build a schema for an XML file to follow.
 /// </summary>
 /// <param name="type">
 /// this is the type that is scanned for a schema
 /// </param>
 public Scanner(Class type)
 {
     this.scanner    = new ClassScanner(type);
     this.attributes = new LabelMap(this);
     this.elements   = new LabelMap(this);
     this.comparer   = new Comparer();
     this.type       = type;
     this.Scan(type);
 }
示例#7
0
 /// <summary>
 /// Applies the LabelMap to the label.  If the LabelMap is null, or does not have an
 /// entry for the label, the original label is returned.
 /// </summary>
 /// <param name="label">Label to convert.</param>
 /// <returns>New label, or original label.</returns>
 public string ConvLabel(string label)
 {
     if (LabelMap != null)
     {
         if (LabelMap.TryGetValue(label, out string newLabel))
         {
             label = newLabel;
         }
     }
     return(label);
 }
示例#8
0
        public void CanInitialiseLabelMapWithLabels()
        {
            var labels = new LabelMap(new Label[] {
                new Label("label1", 0x0001),
                new Label("label2", 0x0002)
            });

            Assert.AreEqual(2, labels.AddressLabels.Count);
            Assert.AreEqual(2, labels.LabelAddresses.Count);
            ushort ignore;

            Assert.IsTrue(labels.TryLookup("label2", out ignore));
        }
示例#9
0
 /// <summary>
 /// Constructor for the <c>Schema</c> object. This is used
 /// to wrap the element and attribute XML annotations scanned from
 /// a class schema. The schema tracks all fields visited so that
 /// a converter can determine if all fields have been serialized.
 /// </summary>
 /// <param name="schema">
 /// this contains all labels scanned from the class
 /// </param>
 /// <param name="context">
 /// this is the context object for serialization
 /// </param>
 public ClassSchema(Scanner schema, Context context)
 {
     this.attributes = schema.getAttributes(context);
     this.elements   = schema.getElements(context);
     this.caller     = schema.getCaller(context);
     this.factory    = schema.Creator;
     this.revision   = schema.Revision;
     this.decorator  = schema.Decorator;
     this.primitive  = schema.IsPrimitive();
     this.version    = schema.Version;
     this.text       = schema.Text;
     this.type       = schema.Type;
 }
        internal override string GetDetails(Prediction prediction, Dictionary <string, string> attFeatureIdInformation)
        {
            StringBuilder report = new StringBuilder("Details for model created by prediction \"" + prediction.Name + "\"" + Environment.NewLine);
            Dictionary <int, Dictionary <int, double> > classFeatureWeight = LibLinearClassifier.GetFeatureWeights(Path.Combine(Model.ModelDirectory, LibLinearClassifier.ModelFileName));
            LabelMap labelMap = new LabelMap(Path.Combine(Model.ModelDirectory, LibLinearClassifier.LabelMapFileName));
            MemoryNumericFeatureNameTransform featureNameTransform = new MemoryNumericFeatureNameTransform(Path.Combine(Model.ModelDirectory, LibLinearClassifier.FeatureNameTransformFileName));

            Dictionary <int, string> liblinearFeatureNumberAttFeatureId = new Dictionary <int, string>();

            foreach (string attFeatureId in featureNameTransform)
            {
                // nominal features in the ATT have IDs in the transform that include the nominal feature value - trim this off to recover the original ID
                int dashIndex = attFeatureId.IndexOf('-');
                if (dashIndex >= 0)
                {
                    liblinearFeatureNumberAttFeatureId.Add(featureNameTransform.GetFeatureNumber(attFeatureId), attFeatureId.Substring(0, dashIndex));
                }
                else
                {
                    liblinearFeatureNumberAttFeatureId.Add(featureNameTransform.GetFeatureNumber(attFeatureId), attFeatureId);
                }
            }

            Dictionary <string, string> attFeatureIdDesc = new Dictionary <string, string>();

            foreach (PTL.ATT.Models.Feature f in Model.Features)
            {
                attFeatureIdDesc.Add(f.Id, "Feature \"" + f.Description + "\"");
            }

            foreach (int classNumber in classFeatureWeight.Keys.OrderBy(i => i))
            {
                report.AppendLine("\tClass \"" + labelMap.GetUnmappedLabel(classNumber.ToString()) + "\"");

                int maxFeatureNameWidth = classFeatureWeight[classNumber].Keys.Max(f => attFeatureIdDesc[liblinearFeatureNumberAttFeatureId[f]].Length);
                foreach (int liblinearFeatureNumber in classFeatureWeight[classNumber].Keys.OrderBy(f => - Math.Abs(classFeatureWeight[classNumber][f])))
                {
                    string desc         = attFeatureIdDesc[liblinearFeatureNumberAttFeatureId[liblinearFeatureNumber]];
                    double weight       = classFeatureWeight[classNumber][liblinearFeatureNumber];
                    string attFeatureId = liblinearFeatureNumberAttFeatureId[liblinearFeatureNumber];
                    string information  = (attFeatureIdInformation == null || !attFeatureIdInformation.ContainsKey(attFeatureId) ? "" : Environment.NewLine +
                                           "\t\t\tInformation:  " + attFeatureIdInformation[attFeatureId]);

                    report.AppendLine(string.Format("\t\t{0,-" + maxFeatureNameWidth + "}: weight = {1:0.00}", desc, weight) + information + Environment.NewLine);
                }

                report.AppendLine();
            }

            return(report.ToString());
        }
示例#11
0
 public Surveillance(IVideoGrab videoGrab, ITrainDataDAL trainDataDAL, double confidence = 0.5)
 {
     this.videoGrab          = videoGrab;
     this.trainDataDAL       = trainDataDAL;
     this.confidence         = confidence;
     faceEyeDetector         = new FaceEyeDetector("Models\\haarcascade_frontalface_default.xml", "Models\\haarcascade_eye.xml");
     recognitionModule       = new FaceRecognitionModule();
     recognitionModuleSecond = new FaceRecognitionModule();
     detectionModule         = new DetectionModule(faceEmbeddingsModel, faceEmbeddingsSecondModel, confidence);
     door     = new DoorManager();
     labelMap = new LabelMap(trainDataDAL.GetLabelMap());
     videoGrab.ImageGrabbed += OnImageGrabbed;
     PersonDetected         += OnPersonDetected;
 }
示例#12
0
        private static void RunOptions(FaceRecognitionParams facePars)
        {
            var images          = GetImages(facePars.DataSet).ToList();
            var detectionModule = new DetectionModule(facePars);

            var faces = detectionModule
                        .GetFaces(images)
                        .Select(f => (GetPersonName(f.Item1), f.Item2))
                        .ToList();
            var labelMap     = new LabelMap(faces.Select(f => f.Item1).Distinct());
            var labeledFaces = faces.Select(f => (labelMap.Map[f.Item1], f.Item2));

            var testImages        = GetImages(facePars.TestSet).ToList();
            var recognitionModule = new FaceRecognitionModule();

            recognitionModule.Train(labeledFaces.ToList(), facePars.Embeddings);

            foreach (var(name, bytes) in testImages)
            {
                var testImg    = detectionModule.ProcessImage(bytes);
                var prediction = recognitionModule.Predict(testImg);
                Console.WriteLine($"Img name : {name} Prediction: {labelMap.ReverseMap[prediction.Label]}, Dist : {prediction.Distance}");
            }
        }
示例#13
0
 /// <summary>
 /// This <c>readElement</c> method is used for deserialization
 /// of the provided node object using a delegate converter. This is
 /// typically another <c>Composite</c> converter, or if the
 /// node is an attribute a <c>Primitive</c> converter. When
 /// the delegate converter has completed the deserialized value is
 /// assigned to the contact.
 /// </summary>
 /// <param name="node">
 /// this is the node that contains the contact value
 /// </param>
 /// <param name="source">
 /// the type of the object that is being deserialized
 /// </param>
 /// <param name="map">
 /// this is the map that contains the label objects
 /// </param>
 public void ReadElement(InputNode node, Object source, LabelMap map) {
    String name = node.GetName();
    Label label = map.take(name);
    if(label == null) {
       label = criteria.Get(name);
    }
    if(label == null) {
       Position line = node.getPosition();
       Class type = source.getClass();
       if(map.IsStrict(context) && revision.IsEqual()) {
          throw new ElementException("Element '%s' does not have a match in %s at %s", name, type, line);
       } else {
          node.skip();
       }
    } else {
       Read(node, source, label);
    }
 }
示例#14
0
 /// <summary>
 /// This <c>validateAttribute</c> method performs a validation
 /// of the provided node object using a delegate converter. This is
 /// typically another <c>Composite</c> converter, or if the
 /// node is an attribute a <c>Primitive</c> converter. If this
 /// fails validation then an exception is thrown to report the issue.
 /// </summary>
 /// <param name="node">
 /// this is the node that contains the contact value
 /// </param>
 /// <param name="map">
 /// this is the map that contains the label objects
 /// </param>
 public void ValidateAttribute(InputNode node, LabelMap map) {
    Position line = node.getPosition();
    String name = node.GetName();
    Label label = map.take(name);
    if(label == null) {
       if(map.IsStrict(context) && revision.IsEqual()) {
          throw new AttributeException("Attribute '%s' does not exist at %s", name, line);
       }
    } else {
       Validate(node, label);
    }
 }
示例#15
0
        /// <summary>
        /// Adjusts the label map so that only local variables start with an underscore ('_').
        /// This is necessary for assemblers like 64tass that use a leading underscore to
        /// indicate that a label should be local.
        ///
        /// This may be called even if label localization is disabled.  In that case we just
        /// create an empty label map and populate as needed.
        ///
        /// Only call this if underscores are used to indicate local labels.
        /// </summary>
        public void MaskLeadingUnderscores()
        {
            bool allGlobal = false;

            if (LabelMap == null)
            {
                allGlobal = true;
                LabelMap  = new Dictionary <string, string>();
            }

            // Throw out the original local label generation.
            LabelMap.Clear();

            // Use this to test for uniqueness.  We add all labels here as we go, not just the
            // ones being remapped.  For each label we either add the original or the localized
            // form.
            SortedList <string, string> allLabels = new SortedList <string, string>();

            for (int i = 0; i < mProject.FileDataLength; i++)
            {
                Symbol sym = mProject.GetAnattrib(i).Symbol;
                if (sym == null)
                {
                    // No label at this offset.
                    continue;
                }

                string newLabel;
                if (allGlobal || mGlobalFlags[i])
                {
                    // Global symbol.  Don't let it start with '_'.
                    if (sym.Label.StartsWith("_"))
                    {
                        // There's an underscore here that was added by the user.  Stick some
                        // other character in front.
                        newLabel = "X" + sym.Label;
                    }
                    else
                    {
                        // No change needed.
                        newLabel = sym.Label;
                    }
                }
                else
                {
                    // Local symbol.
                    if (sym.Label.StartsWith("_"))
                    {
                        // The original starts with one or more underscores.  Adding another
                        // will create a "__" label, which is reserved in 64tass.
                        newLabel = "_X" + sym.Label;
                    }
                    else
                    {
                        newLabel = "_" + sym.Label;
                    }
                }

                // Make sure it's unique.
                string uniqueLabel = newLabel;
                int    uval        = 1;
                while (allLabels.ContainsKey(uniqueLabel))
                {
                    uniqueLabel = newLabel + uval.ToString();
                }
                allLabels.Add(uniqueLabel, uniqueLabel);

                // If it's different, add it to the label map.
                if (sym.Label != uniqueLabel)
                {
                    LabelMap.Add(sym.Label, uniqueLabel);
                }
            }

            Debug.WriteLine("UMAP: allcount=" + allLabels.Count + " mapcount=" + LabelMap.Count);
        }
示例#16
0
 /// <summary>
 /// Constructor for the <c>Scanner</c> object. This is used
 /// to scan the provided class for annotations that are used to
 /// build a schema for an XML file to follow.
 /// </summary>
 /// <param name="type">
 /// this is the type that is scanned for a schema
 /// </param>
 public Scanner(Class type) {
    this.scanner = new ClassScanner(type);
    this.attributes = new LabelMap(this);
    this.elements = new LabelMap(this);
    this.comparer = new Comparer();
    this.type = type;
    this.Scan(type);
 }
示例#17
0
        /// <summary>
        /// Remaps labels that match opcode names.  Updated names will be added to LabelMap.
        /// This should be run after localization and underscore concealment have finished.
        /// </summary>
        /// <remarks>
        /// Most assemblers don't like it if you create a label with the same name as an
        /// opcode, e.g. "jmp LSR" doesn't work.  We can use the label map to work around
        /// the issue.
        ///
        /// Most assemblers regard mnemonics as case-insensitive, even if labels are
        /// case-sensitive, so we want to remap both "lsr" and "LSR".
        ///
        /// This doesn't really have anything to do with label localization other than that
        /// we're updating the label remap table.
        /// </remarks>
        public void FixOpcodeLabels()
        {
            if (LabelMap == null)
            {
                LabelMap = new Dictionary <string, string>();
            }

            // Create a searchable list of opcode names using the current CPU definition.
            // (All tested assemblers that failed on opcode names only did so for names
            // that were part of the current definition, e.g. "TSB" was accepted as a label
            // when the CPU was set to 6502.)
            Dictionary <string, Asm65.OpDef> opnames = new Dictionary <string, Asm65.OpDef>();

            Asm65.CpuDef cpuDef = mProject.CpuDef;
            for (int i = 0; i < 256; i++)
            {
                Asm65.OpDef op = cpuDef.GetOpDef(i);
                // There may be multiple entries with the same name (e.g. "NOP").  That's fine.
                opnames[op.Mnemonic.ToUpperInvariant()] = op;
            }

            // Create a list of all labels, for uniqueness testing.  If a label has been
            // remapped, we add the remapped entry.
            // (All tested assemblers that failed on opcode names only did so for names
            // in their non-localized form.  While "LSR" failed, "@LSR", "_LSR", ".LSR", etc.
            // were accepted.  So if it  was remapped by the localizer, we don't need to
            // worry about it.)
            SortedList <string, string> allLabels = new SortedList <string, string>();

            for (int i = 0; i < mProject.FileDataLength; i++)
            {
                Symbol sym = mProject.GetAnattrib(i).Symbol;
                if (sym == null)
                {
                    continue;
                }
                LabelMap.TryGetValue(sym.Label, out string mapLabel);
                if (mapLabel != null)
                {
                    allLabels.Add(mapLabel, mapLabel);
                }
                else
                {
                    allLabels.Add(sym.Label, sym.Label);
                }
            }

            // Now run through the list of labels, looking for any that match opcode
            // mnemonics.
            for (int i = 0; i < mProject.FileDataLength; i++)
            {
                Symbol sym = mProject.GetAnattrib(i).Symbol;
                if (sym == null)
                {
                    // No label at this offset.
                    continue;
                }
                string cmpLabel = sym.Label;
                if (LabelMap.TryGetValue(sym.Label, out string mapLabel))
                {
                    cmpLabel = mapLabel;
                }

                if (opnames.ContainsKey(cmpLabel.ToUpperInvariant()))
                {
                    //Debug.WriteLine("Remapping label (op mnemonic): " + sym.Label);

                    int    uval = 0;
                    string uniqueLabel;
                    do
                    {
                        uval++;
                        uniqueLabel = cmpLabel + "_" + uval.ToString();
                    } while (allLabels.ContainsKey(uniqueLabel));

                    allLabels.Add(uniqueLabel, uniqueLabel);
                    LabelMap.Add(sym.Label, uniqueLabel);
                }
            }

            if (LabelMap.Count == 0)
            {
                // didn't do anything, lose the table
                LabelMap = null;
            }
        }
示例#18
0
 /// <summary>
 /// This method checks to see if there are any <c>Label</c>
 /// objects remaining in the provided map that are required. This is
 /// used when validation is performed to ensure the the XML element
 /// validated contains sufficient details to satisfy the XML schema
 /// class annotations. If there is a required label that remains it
 /// is reported within the exception thrown.
 /// </summary>
 /// <param name="node">
 /// this is the node that contains the composite data
 /// </param>
 /// <param name="map">
 /// this contains the converters to perform validation
 /// </param>
 public void Validate(InputNode node, LabelMap map) {
    Position line = node.getPosition();
    for(Label label : map) {
       if(label.isRequired() && revision.IsEqual()) {
          throw new ValueRequiredException("Unable to satisfy %s at %s", label, line);
       }
    }
 }
示例#19
0
 /// <summary>
 /// This <c>validateElement</c> method performs a validation
 /// of the provided node object using a delegate converter. This is
 /// typically another <c>Composite</c> converter, or if the
 /// node is an attribute a <c>Primitive</c> converter. If this
 /// fails validation then an exception is thrown to report the issue.
 /// </summary>
 /// <param name="node">
 /// this is the node that contains the contact value
 /// </param>
 /// <param name="map">
 /// this is the map that contains the label objects
 /// </param>
 public void ValidateElement(InputNode node, LabelMap map) {
    String name = node.GetName();
    Label label = map.take(name);
    if(label == null) {
       label = criteria.Get(name);
    }
    if(label == null) {
       Position line = node.getPosition();
       if(map.IsStrict(context) && revision.IsEqual()) {
          throw new ElementException("Element '%s' does not exist at %s", name, line);
       } else {
          node.skip();
       }
    } else {
       Validate(node, label);
    }
 }
示例#20
0
        /// <summary>
        /// Create the dataset and load it into the database.
        /// </summary>
        /// <param name="nCreatorID">Specifies the creator ID.</param>
        /// <returns>On successful creation, <i>true</i> is returned, otherwise <i>false</i> is returned on abort.</returns>
        public bool LoadDatabase(int nCreatorID = 0)
        {
            try
            {
                reportProgress(0, 0, "Loading " + dataset_name + " database...");

                int nIdx          = 0;
                int nTotal        = 5011 + 17125;
                int nExtractIdx   = 0;
                int nExtractTotal = 10935 + 40178;

                // Get the label map.
                LabelMap labelMap = loadLabelMap();
                Dictionary <string, int> rgNameToLabel = labelMap.MapToLabel(m_log, true);
                string strSrc = dataset_name + ".training";

                int nSrcId = m_factory.GetSourceID(strSrc);
                if (nSrcId > 0)
                {
                    m_factory.DeleteSourceData(nSrcId);
                }

                List <Tuple <int, string, Size> > rgFileSizes = new List <Tuple <int, string, Size> >();

                if (!loadFile(m_param.DataBatchFileTrain2007, strSrc, nExtractTotal, ref nExtractIdx, nTotal, ref nIdx, m_log, m_param.ExtractFiles, rgNameToLabel, rgFileSizes))
                {
                    return(false);
                }

                if (!loadFile(m_param.DataBatchFileTrain2012, strSrc, nExtractTotal, ref nExtractIdx, nTotal, ref nIdx, m_log, m_param.ExtractFiles, rgNameToLabel, rgFileSizes))
                {
                    return(false);
                }

                string strDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\MyCaffe\\test_data\\data\\ssd\\VOC0712\\";
                if (!Directory.Exists(strDir))
                {
                    Directory.CreateDirectory(strDir);
                }

                saveFileSizes(rgFileSizes, strDir + "train_name_size.txt");

                SourceDescriptor srcTrain = m_factory.LoadSource(strSrc);

                rgFileSizes   = new List <Tuple <int, string, Size> >();
                m_rgImg       = new List <SimpleDatum>();
                nIdx          = 0;
                nTotal        = 4952;
                nExtractIdx   = 0;
                nExtractTotal = 10347;

                strSrc = dataset_name + ".testing";

                nSrcId = m_factory.GetSourceID(strSrc);
                if (nSrcId > 0)
                {
                    m_factory.DeleteSourceData(nSrcId);
                }

                if (!loadFile(m_param.DataBatchFileTest2007, strSrc, nExtractTotal, ref nExtractIdx, nTotal, ref nIdx, m_log, m_param.ExtractFiles, rgNameToLabel, rgFileSizes))
                {
                    return(false);
                }

                saveFileSizes(rgFileSizes, strDir + "test_name_size.txt");

                SourceDescriptor srcTest = m_factory.LoadSource(strSrc);

                DatasetDescriptor ds = new DatasetDescriptor(nCreatorID, dataset_name, null, null, srcTrain, srcTest, dataset_name, dataset_name + " Dataset");
                m_factory.AddDataset(ds);
                m_factory.UpdateDatasetCounts(ds.ID);

                return(true);
            }
            catch (Exception excpt)
            {
                throw excpt;
            }
            finally
            {
                if (OnCompleted != null)
                {
                    OnCompleted(this, new EventArgs());
                }
            }
        }
        internal override string GetDetails(Prediction prediction, Dictionary<string, string> attFeatureIdInformation)
        {
            StringBuilder report = new StringBuilder("Details for model created by prediction \"" + prediction.Name + "\"" + Environment.NewLine);
            Dictionary<int, Dictionary<int, double>> classFeatureWeight = LibLinearClassifier.GetFeatureWeights(Path.Combine(Model.ModelDirectory, LibLinearClassifier.ModelFileName));
            LabelMap labelMap = new LabelMap(Path.Combine(Model.ModelDirectory, LibLinearClassifier.LabelMapFileName));
            MemoryNumericFeatureNameTransform featureNameTransform = new MemoryNumericFeatureNameTransform(Path.Combine(Model.ModelDirectory, LibLinearClassifier.FeatureNameTransformFileName));

            Dictionary<int, string> liblinearFeatureNumberAttFeatureId = new Dictionary<int, string>();
            foreach (string attFeatureId in featureNameTransform)
            {
                // nominal features in the ATT have IDs in the transform that include the nominal feature value - trim this off to recover the original ID
                int dashIndex = attFeatureId.IndexOf('-');
                if (dashIndex >= 0)
                    liblinearFeatureNumberAttFeatureId.Add(featureNameTransform.GetFeatureNumber(attFeatureId), attFeatureId.Substring(0, dashIndex));
                else
                    liblinearFeatureNumberAttFeatureId.Add(featureNameTransform.GetFeatureNumber(attFeatureId), attFeatureId);
            }

            Dictionary<string, string> attFeatureIdDesc = new Dictionary<string, string>();
            foreach (PTL.ATT.Models.Feature f in Model.Features)
                attFeatureIdDesc.Add(f.Id, "Feature \"" + f.Description + "\"");

            foreach (int classNumber in classFeatureWeight.Keys.OrderBy(i => i))
            {
                report.AppendLine("\tClass \"" + labelMap.GetUnmappedLabel(classNumber.ToString()) + "\"");

                int maxFeatureNameWidth = classFeatureWeight[classNumber].Keys.Max(f => attFeatureIdDesc[liblinearFeatureNumberAttFeatureId[f]].Length);
                foreach (int liblinearFeatureNumber in classFeatureWeight[classNumber].Keys.OrderBy(f => -Math.Abs(classFeatureWeight[classNumber][f])))
                {
                    string desc = attFeatureIdDesc[liblinearFeatureNumberAttFeatureId[liblinearFeatureNumber]];
                    double weight = classFeatureWeight[classNumber][liblinearFeatureNumber];
                    string attFeatureId = liblinearFeatureNumberAttFeatureId[liblinearFeatureNumber];
                    string information = (attFeatureIdInformation == null || !attFeatureIdInformation.ContainsKey(attFeatureId) ? "" : Environment.NewLine +
                                         "\t\t\tInformation:  " + attFeatureIdInformation[attFeatureId]);

                    report.AppendLine(string.Format("\t\t{0,-" + maxFeatureNameWidth + "}: weight = {1:0.00}", desc, weight) + information + Environment.NewLine);
                }

                report.AppendLine();
            }

            return report.ToString();
        }
示例#22
0
 /// <summary>
 /// This method checks to see if there are any <c>Label</c>
 /// objects remaining in the provided map that are required. This is
 /// used when deserialization is performed to ensure the the XML
 /// element deserialized contains sufficient details to satisfy the
 /// XML schema class annotations. If there is a required label that
 /// remains it is reported within the exception thrown.
 /// </summary>
 /// <param name="map">
 /// this is the map to check for remaining labels
 /// </param>
 /// <param name="source">
 /// this is the object that has been deserialized
 /// </param>
 public void Validate(InputNode node, LabelMap map, Object source) {
    Position line = node.getPosition();
    Class expect = type.Type;
    if(source != null) {
       expect = source.getClass();
    }
    for(Label label : map) {
       if(label.isRequired() && revision.IsEqual()) {
          throw new ValueRequiredException("Unable to satisfy %s for %s at %s", label, expect, line);
       }
       Object value = label.getEmpty(context);
       if(value != null) {
          criteria.Set(label, value);
       }
    }
 }
示例#23
0
        /// <summary>
        /// Create the dataset and load it into the database.
        /// </summary>
        /// <param name="nCreatorID">Specifies the creator ID.</param>
        /// <returns>On successful creation, <i>true</i> is returned, otherwise <i>false</i> is returned on abort.</returns>
        public bool LoadDatabase(int nCreatorID = 0)
        {
            try
            {
                reportProgress(0, 0, "Loading " + dataset_name + " database...");

                int nIdx          = 0;
                int nTotal        = 5011 + 17125;
                int nExtractIdx   = 0;
                int nExtractTotal = 10935 + 40178;

                // Get the label map.
                LabelMap labelMap = loadLabelMap();
                Dictionary <string, int> rgNameToLabel = labelMap.MapToLabel(m_log, true);
                string strSrc = dataset_name + ".training";

                int nSrcId = m_factory.GetSourceID(strSrc);
                if (nSrcId > 0)
                {
                    m_factory.DeleteSourceData(nSrcId);
                }

                if (!loadFile(m_param.DataBatchFileTrain2007, strSrc, nExtractTotal, ref nExtractIdx, nTotal, ref nIdx, m_log, m_param.ExtractFiles, rgNameToLabel))
                {
                    return(false);
                }

                if (!loadFile(m_param.DataBatchFileTrain2012, strSrc, nExtractTotal, ref nExtractIdx, nTotal, ref nIdx, m_log, m_param.ExtractFiles, rgNameToLabel))
                {
                    return(false);
                }

                SourceDescriptor srcTrain = m_factory.LoadSource(strSrc);

                m_rgImg       = new List <SimpleDatum>();
                nIdx          = 0;
                nTotal        = 4952;
                nExtractIdx   = 0;
                nExtractTotal = 10347;

                strSrc = dataset_name + ".testing";

                nSrcId = m_factory.GetSourceID(strSrc);
                if (nSrcId > 0)
                {
                    m_factory.DeleteSourceData(nSrcId);
                }

                if (!loadFile(m_param.DataBatchFileTest2007, strSrc, nExtractTotal, ref nExtractIdx, nTotal, ref nIdx, m_log, m_param.ExtractFiles, rgNameToLabel))
                {
                    return(false);
                }

                SourceDescriptor srcTest = m_factory.LoadSource(strSrc);

                DatasetDescriptor ds = new DatasetDescriptor(nCreatorID, dataset_name, null, null, srcTrain, srcTest, dataset_name, dataset_name + " Dataset");
                m_factory.AddDataset(ds);
                m_factory.UpdateDatasetCounts(ds.ID);

                return(true);
            }
            catch (Exception excpt)
            {
                throw excpt;
            }
            finally
            {
                if (OnCompleted != null)
                {
                    OnCompleted(this, new EventArgs());
                }
            }
        }
示例#24
0
 /// <summary>
 /// Constructor for the <c>Schema</c> object. This is used
 /// to wrap the element and attribute XML annotations scanned from
 /// a class schema. The schema tracks all fields visited so that
 /// a converter can determine if all fields have been serialized.
 /// </summary>
 /// <param name="schema">
 /// this contains all labels scanned from the class
 /// </param>
 /// <param name="context">
 /// this is the context object for serialization
 /// </param>
 public ClassSchema(Scanner schema, Context context) {
    this.attributes = schema.getAttributes(context);
    this.elements = schema.getElements(context);
    this.caller = schema.getCaller(context);
    this.factory = schema.Creator;
    this.revision = schema.Revision;
    this.decorator = schema.Decorator;
    this.primitive = schema.IsPrimitive();
    this.version = schema.Version;
    this.text = schema.Text;
    this.type = schema.Type;
 }
示例#25
0
        /// <summary>
        /// Setup the layer.
        /// </summary>
        /// <param name="colBottom">Specifies the collection of bottom (input) Blobs.</param>
        /// <param name="colTop">Specifies the collection of top (output) Blobs.</param>
        public override void LayerSetUp(BlobCollection <T> colBottom, BlobCollection <T> colTop)
        {
            m_log.CHECK_GT(m_param.detection_output_param.num_classes, 0, "There must be at least one class specified.");
            m_nNumClasses = (int)m_param.detection_output_param.num_classes;

            m_bShareLocations          = m_param.detection_output_param.share_location;
            m_nNumLocClasses           = (m_bShareLocations) ? 1 : m_nNumClasses;
            m_nBackgroundLabelId       = m_param.detection_output_param.background_label_id;
            m_codeType                 = m_param.detection_output_param.code_type;
            m_bVarianceEncodedInTarget = m_param.detection_output_param.variance_encoded_in_target;
            m_nKeepTopK                = m_param.detection_output_param.keep_top_k;
            m_fConfidenceThreshold     = m_param.detection_output_param.confidence_threshold.GetValueOrDefault(-float.MaxValue);

            // Parameters used in nms.
            m_fNmsThreshold = m_param.detection_output_param.nms_param.nms_threshold;
            m_log.CHECK_GE(m_fNmsThreshold, 0, "The nms_threshold must be non negative.");
            m_fEta = m_param.detection_output_param.nms_param.eta;
            m_log.CHECK_GT(m_fEta, 0, "The nms_param.eta must be > 0.");
            m_log.CHECK_LE(m_fEta, 1, "The nms_param.eta must be < 0.");

            m_nTopK = m_param.detection_output_param.nms_param.top_k.GetValueOrDefault(-1);

            m_strOutputDir = m_param.detection_output_param.save_output_param.output_directory;
            m_bNeedSave    = !string.IsNullOrEmpty(m_strOutputDir);
            if (m_bNeedSave && !Directory.Exists(m_strOutputDir))
            {
                Directory.CreateDirectory(m_strOutputDir);
            }

            m_strOutputNamePrefix = m_param.detection_output_param.save_output_param.output_name_prefix;
            m_outputFormat        = m_param.detection_output_param.save_output_param.output_format;

            if (!string.IsNullOrEmpty(m_param.detection_output_param.save_output_param.label_map_file))
            {
                string strLabelMapFile = m_param.detection_output_param.save_output_param.label_map_file;
                if (!File.Exists(strLabelMapFile))
                {
                    // Ignore saving if there is no label map file.
                    m_log.WriteLine("WARNING: Could not find the label_map_file '" + strLabelMapFile + "'!");
                    m_bNeedSave = false;
                }
                else
                {
                    LabelMap label_map;

                    try
                    {
                        RawProto proto = RawProto.FromFile(strLabelMapFile);
                        label_map = LabelMap.FromProto(proto);
                    }
                    catch (Exception excpt)
                    {
                        throw new Exception("Failed to read label map file!", excpt);
                    }

                    try
                    {
                        m_rgLabelToName = label_map.MapToName(m_log, true, false);
                    }
                    catch (Exception excpt)
                    {
                        throw new Exception("Failed to convert the label to name!", excpt);
                    }

                    try
                    {
                        m_rgLabelToDisplayName = label_map.MapToName(m_log, true, true);
                    }
                    catch (Exception excpt)
                    {
                        throw new Exception("Failed to convert the label to display name!", excpt);
                    }
                }
            }
            else
            {
                m_bNeedSave = false;
            }

            if (!string.IsNullOrEmpty(m_param.detection_output_param.save_output_param.name_size_file))
            {
                string strNameSizeFile = m_param.detection_output_param.save_output_param.name_size_file;
                if (!File.Exists(strNameSizeFile))
                {
                    // Ignore saving if there is no name size file.
                    m_log.WriteLine("WARNING: Could not find the name_size_file '" + strNameSizeFile + "'!");
                    m_bNeedSave = false;
                }
                else
                {
                    using (StreamReader sr = new StreamReader(strNameSizeFile))
                    {
                        string strName;
                        int    nHeight;
                        int    nWidth;

                        string strLine = sr.ReadLine();
                        while (strLine != null)
                        {
                            string[] rgstr = strLine.Split(' ');
                            if (rgstr.Length != 3 && rgstr.Length != 4)
                            {
                                throw new Exception("Invalid name_size_file format, expected 'name' 'height' 'width'");
                            }

                            int nNameIdx = (rgstr.Length == 4) ? 1 : 0;
                            strName = rgstr[nNameIdx].Trim(',');
                            nHeight = int.Parse(rgstr[nNameIdx + 1].Trim(','));
                            nWidth  = int.Parse(rgstr[nNameIdx + 2].Trim(','));

                            m_rgstrNames.Add(strName);
                            m_rgSizes.Add(new SizeF(nWidth, nHeight));

                            strLine = sr.ReadLine();
                        }
                    }

                    if (m_param.detection_output_param.save_output_param.num_test_image.HasValue)
                    {
                        m_nNumTestImage = (int)m_param.detection_output_param.save_output_param.num_test_image.Value;
                    }
                    else
                    {
                        m_nNumTestImage = m_rgstrNames.Count;
                    }

                    m_log.CHECK_LE(m_nNumTestImage, m_rgstrNames.Count, "The number of test images cannot exceed the number of names.");
                }
            }
            else
            {
                m_bNeedSave = false;
            }

            if (m_param.detection_output_param.save_output_param.resize_param != null && m_param.detection_output_param.save_output_param.resize_param.Active)
            {
                m_resizeParam = m_param.detection_output_param.save_output_param.resize_param;
            }

            m_nNameCount = 0;

            m_bVisualize = m_param.detection_output_param.visualize;
            if (m_bVisualize)
            {
                m_fVisualizeThreshold = m_param.detection_output_param.visualize_threshold.GetValueOrDefault(0.6f);
                m_transformer         = new DataTransformer <T>(m_cuda, m_log, m_param.transform_param, m_phase, 0, 0, 0);
                m_transformer.InitRand();
                m_strSaveFile = m_param.detection_output_param.save_file;
            }

            m_blobBboxPreds.ReshapeLike(colBottom[0]);

            if (!m_bShareLocations)
            {
                m_blobBboxPermute.ReshapeLike(colBottom[0]);
            }

            m_blobConfPermute.ReshapeLike(colBottom[1]);
        }
示例#26
0
        /// <summary>
        /// スクリプトコードを構文解析して,実行可能
        /// </summary>
        /// <returns>The parse.</returns>
        /// <param name="code">Code.</param>
        /// <param name="echoCommand">Echo command.</param>
        public static INCode Parse(string code, string echoCommand = "say")
        {
            // スクリプトをステートメントに区切る
            var statements = code.SplitLine();

            // 汎用文字列バッファ
            var buffer = new StringBuilder();

            // 文字列リテラルかどうか
            var isQuotation = false;

            // 解析用トークン
            var token = Token.SpriteTag;

            // 一時保存変数の定義
            string spTag, comName;
            var    args = new List <string>();

            // ステートメント
            var statementList = new List <INStatement>();

            // ラベル一覧
            var labels = new LabelMap();

            // 行番号
            var line = 0;

            foreach (var statement in statements)
            {
                // 一時保存変数やフラグの初期化
                spTag = comName = "";
                args?.Clear();
                token       = Token.SpriteTag;
                isQuotation = false;

                // ここからステートメントの解析
                for (var i = 0; i < statement.Length; i++)
                {
                    // 現在パースしている文字
                    var chr = statement[i];
                    // chrの1つ前またはnull文字
                    var cm1 = (i > 0) ? statement[i - 1] : '\0';
                    // chrの1つ後またはnull文字
                    var cp1 = (i < statement.Length - 1) ? statement[i + 1] : '\0';

                    // リテラル外において,空白などは無視される
                    if ((!isQuotation) && (char.IsControl(chr) || char.IsSeparator(chr) || char.IsWhiteSpace(chr)))
                    {
                        continue;
                    }

                    // コメント文
                    if ((!isQuotation) && chr == '/' && cp1 == '/')
                    {
                        break;
                    }

                    // 解析処理
                    switch (token)
                    {
                    // スプライトタグ
                    case Token.SpriteTag:
                        // 現在地がプレフィックスであれば抜ける
                        if (chr == COMMAND_PREFIX || chr == MESSAGE_PREFIX || chr == LABEL_PREFIX)
                        {
                            // スプライトタグをバッファの文字列に設定する
                            spTag = buffer.ToString();

                            // トークンの切り替え
                            token = Token.Prefix;

                            // バッファの掃除
                            buffer.Clear();

                            // 次の段階でプレフィックスのチェックをするためiを前にずらす (for文によるiの増加に合わせる)
                            i--;

                            // 次にすっ飛ばす
                            continue;
                        }

                        //読み取った文字をバッファに入れる
                        buffer.Append(chr);
                        break;

                    // プレフィックスの解析
                    case Token.Prefix:
                        switch (chr)
                        {
                        case COMMAND_PREFIX:
                            // コマンドの解析に飛ぶ
                            token = Token.Name;
                            continue;

                        case MESSAGE_PREFIX:
                            // メッセージコマンドということにして
                            comName = echoCommand;
                            // 引数解析にすっ飛ばす
                            token = Token.Arguments;
                            continue;

                        case LABEL_PREFIX:
                            // コマンドではない処理を行う
                            token = Token.LabelName;
                            continue;

                        default:
                            throw new ParseStatementException($"Invalid prefix {(chr != '\0' ? chr.ToString() : "")}", line, i);
                        }

                    // コマンド名の解析
                    case Token.Name:
                        //読み取った文字をバッファに入れる
                        buffer.Append(chr);

                        // 区切り文字が登場したら
                        if (cp1 == COMMAND_SEPARATOR)
                        {
                            // コマンド名を設定して
                            comName = buffer.ToString();
                            // バッファをお掃除して
                            buffer.Clear();
                            // 区切り文字の分カーソルを進めて
                            i++;
                            // トークンを切り替えて
                            token = Token.Arguments;
                            // イッテヨシ
                            continue;
                        }
                        break;

                    // 引数の構文解析
                    case Token.Arguments:

                        switch (chr)
                        {
                        // エスケープシーケンスの処理
                        case '\\':
                            switch (cp1)
                            {
                            // 改行
                            case 'n':
                                buffer = buffer.Append('\n');
                                break;

                            // ダブルクォート
                            case '"':
                                buffer = buffer.Append('"');
                                break;

                            // バックスラッシュ
                            case '\\':
                                buffer = buffer.Append('\\');
                                break;

                            // 予期しないやつ
                            default:
                                throw new ParseStatementException($@"Invalid escape sequence \{(cp1 != '\0' ? cp1.ToString() : "")}", line, i);
                            }
                            // エスケープ文字分飛ばす
                            i++;
                            break;

                        // ダブルクォート
                        case '"':
                            // ダブルクォートで包んだ文字列は,空白や,の影響を受けない
                            isQuotation = !isQuotation;
                            break;

                        // その他の文字
                        default:
                            // 引数の区切り
                            if ((chr == ',' && !isQuotation))
                            {
                                args?.Add(buffer.ToString());
                                buffer.Clear();
                            }
                            // 読み取った引数の文字をバッファに入れる
                            else
                            {
                                buffer.Append(chr);
                            }
                            break;
                        }
                        break;

                    // ラベル名
                    case Token.LabelName:
                        // 念のためステートメントとして登録できないようコマンド名を初期化
                        comName = "";
                        buffer.Append(chr);

                        break;

                    // 異常なトークン(プログラムのバグでない限りありえない)
                    default:
                        throw new ParseStatementException($@"(Bug!!) Unknown token ""{token}""", line, i);
                    }
                }

                // バッファが空でない場合,データが残っているので加味
                if (!string.IsNullOrEmpty(buffer.ToString()))
                {
                    switch (token)
                    {
                    case Token.Name:
                        comName = buffer.ToString();
                        break;

                    case Token.LabelName:
                        labels.Add(buffer.ToString(), line);
                        break;

                    default:
                        args.Add(buffer.ToString());
                        break;
                    }

                    buffer.Clear();
                }

                // コマンド名がカラッポ = ラベルやコメント行や空行なのでスルー
                if (string.IsNullOrEmpty(comName))
                {
                    continue;
                }

                statementList.Add(new NStatement(comName, spTag, args.ToArray()));

                line++;
            }
            return(new NCode(labels, statementList.ToArray()));
        }