Ejemplo n.º 1
0
        static public TestCollection.DistanceComputeDelegate ParseAlgo(string algo)
        {
            TestCollection.DistanceComputeDelegate ret = Correlation;

            switch (algo.ToLower())
            {
            case "corr":
                ret = Correlation;
                break;

            case "l1":
                ret = L1Distance;
                break;

            case "l2":
                ret = L2Distance;
                break;

            case "lde":
                //LDEDistance ldeDist = new LDEDistance("Projection.bin", 100);
                //ret = ldeDist.Distance;
                break;

            default:
                Console.WriteLine("Unrecognized algorithm {0}", algo);
                ret = null;
                break;
            }

            return(ret);
        }
Ejemplo n.º 2
0
        public void DoDistances(List <Face> FaceList, TestCollection.DistanceComputeDelegate DistanceComputer)
        {
            _distances = new DistanceMeasure[FaceList.Count];

            for (int otherFaceCount = 0; otherFaceCount < FaceList.Count; ++otherFaceCount)
            {
                Face otherFace = FaceList[otherFaceCount];
                _distances[otherFaceCount].Distance = DistanceComputer(this.Data, otherFace.Data);
                _distances[otherFaceCount].face     = otherFace;
            }
        }
Ejemplo n.º 3
0
        static void TestFaceUsingSort(Face face, int kMax, int kVal, TestCollection.DistanceComputeDelegate distanceComputer)
        {
            if (kMax <= 0)
            {
                kMax = face.Distances.Length;
            }
            else
            {
                kMax = Math.Min(kMax, face.Distances.Length);
            }

            Dictionary <int, int> classesSeen = new Dictionary <int, int>();

            Face[] faceArray = new Face[kMax];
            int    iFace     = 0;

            for (int k = 0; k < kMax; ++k)
            {
                DistanceMeasure d         = face.Distances[k];
                Face            otherFace = d.face;

                if (otherFace.ID == face.ID)
                {
                    continue;
                }

                if (false == classesSeen.ContainsKey(otherFace.GroupId))
                {
                    classesSeen.Add(otherFace.GroupId, 0);
                }
                else
                {
                    classesSeen[otherFace.GroupId]++;
                }

                if (classesSeen[otherFace.GroupId] < kVal)
                {
                    faceArray[iFace++] = otherFace;
                }
            }

            Array.Resize(ref faceArray, iFace);

            FacePairSorter facePairSorter = new FacePairSorter(distanceComputer);

            facePairSorter.RefFace = face;

            Array.Sort(faceArray, facePairSorter);
            Console.Write("[ {0} {1} {2} ]: ", face.GroupId, face.ID, faceArray[0].GroupId);
            Console.Write("{0}", faceArray[0].ID);
            Console.WriteLine();
        }
Ejemplo n.º 4
0
        static void RunFiltered(int kMax, List <int> kVals, TestCollection testUnLabel, TestCollection.DistanceComputeDelegate distanceComputer, int verbose)
        {
            List <Face> otherFaces = new List <Face>();

            foreach (Face face in testUnLabel.Faces)
            {
                otherFaces.Clear();
                kMax = Math.Min(kMax, face.Distances.Length);

                for (int k = 0; k < kMax; ++k)
                {
                    if (face.ID != face.Distances[k].face.ID)
                    {
                        otherFaces.Add(face.Distances[k].face);
                    }
                    else
                    {
                        int tmp = 0;
                        ++tmp;
                    }
                }

                face.DoDistances(otherFaces, distanceComputer);

                face.Sort();
            }
        }
Ejemplo n.º 5
0
        static public TestCollection.DistanceComputeDelegate LoadDistanceAlgoFromDll(string dllPath, string methodName, string optionalFileName)
        {
            TestCollection.DistanceComputeDelegate distDelegate = null;
            Assembly dll = null;

            try
            {
                dll = Assembly.LoadFrom(dllPath);
            }
            catch (Exception)
            {
                Console.WriteLine("Cannot load dll {0}", dllPath);
                return(null);
            }

            Type[] args = new Type[2];

            int iArg = 0;

            args[iArg++] = typeof(double[]);
            args[iArg++] = typeof(double[]);

            //BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;

            foreach (Type type in dll.GetTypes())
            {
                MethodInfo methodInfo = type.GetMethod(methodName, args);

                if (null != methodInfo)
                {
                    Object[] parms = new object[1];
                    parms[0] = optionalFileName;
                    Object obj = null;
                    try
                    {
                        if (false == methodInfo.IsStatic)
                        {
                            obj = Activator.CreateInstance(type, parms);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Failed to create an object instance from {0} ", dllPath);
                        if (null != optionalFileName)
                        {
                            Console.WriteLine("with  parameter {0}", optionalFileName);
                        }
                        Console.WriteLine("{0}", e.Message);
                        return(null);
                    }
                    try
                    {
                        distDelegate = Delegate.CreateDelegate(typeof(TestCollection.DistanceComputeDelegate),
                                                               obj, methodInfo, true) as TestCollection.DistanceComputeDelegate;
                        Console.WriteLine("Found {0} in {1}", methodName, dllPath);
                        break;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Failed to bind a delegate to {0}.{1} because of {2}",
                                          type.FullName, methodName, e.Message);
                    }
                }
            }

            if (null == distDelegate)
            {
                Console.WriteLine("Cannot find method {0} in {1}", methodName, dllPath);
            }
            return(distDelegate);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            int           iArg         = 0;
            int           verbose      = 0;
            StreamWriter  sw           = null;
            List <int>    kVals        = new List <int>();
            int           kMax         = 5;
            List <double> rejectThresh = new List <double>();
            List <TestCollection.DistanceComputeDelegate> distanceComputer = new List <TestCollection.DistanceComputeDelegate>();
            bool   dataFilesFound    = false;
            bool   doTrainLabel      = true;
            bool   doDistances       = true;
            string optionalDllString = null;
            string dllLoadPath       = null;
            string dataFileName      = null;
            string dllMethodName     = "Distance";

            TestAction action           = TestAction.Base;
            int        iDistanceCompute = 0;

            if (args.Length < 2)
            {
                Usage();
                return;
            }

            while (dataFilesFound == false && iArg < args.Length - 2)
            {
                switch (args[iArg].ToLower())
                {
                case "-k":
                    ++iArg;
                    kVals.Add(Convert.ToInt32(args[iArg]));
                    break;

                case "-verbose":
                    ++iArg;
                    verbose = Convert.ToInt32(args[iArg]);
                    break;

                case "-algo":
                    ++iArg;
                    distanceComputer.Add(ParseAlgo(args[iArg]));
                    break;

                case "-dllalgo":
                    ++iArg;
                    dllLoadPath = args[iArg];
                    break;

                case "-dllinfo":
                    optionalDllString = args[++iArg];
                    break;

                case "-out":
                    ++iArg;
                    FileStream fs = new FileStream(args[iArg], FileMode.Create);
                    sw = new StreamWriter(fs);
                    break;

                case "-filter":
                    action = TestAction.Filtered;
                    break;

                case "-rejectthresh":
                    ++iArg;
                    rejectThresh.Add(Convert.ToDouble(args[iArg]));
                    break;

                case "-gentraintriplets":
                    action = TestAction.GenerateTrainTriplets;
                    //doTrainLabel = false;
                    doDistances = false;
                    break;

                case "-gentrainpairs":
                    action       = TestAction.GenerateTrainPairs;
                    doTrainLabel = false;
                    doDistances  = false;
                    break;

                case "-generatedepth":
                    action       = TestAction.GenerateDepth;
                    doTrainLabel = false;
                    doDistances  = false;
                    break;

                case "-testpairs":
                    action = TestAction.TestPairs;
                    break;

                case "-reporttrain":
                    action       = TestAction.ReportTrainData;
                    dataFileName = args[++iArg];
                    doTrainLabel = false;
                    doDistances  = false;
                    break;

                case "-dllmethod":
                    ++iArg;
                    dllMethodName = args[iArg];
                    break;

                case "-datafiles":
                    dataFilesFound = true;
                    break;
                }
                ++iArg;
            }

            if (null != dllLoadPath)
            {
                TestCollection.DistanceComputeDelegate distanceDelegate = LoadDistanceAlgoFromDll(dllLoadPath, dllMethodName, optionalDllString);
                if (null != distanceDelegate)
                {
                    distanceComputer.Add(distanceDelegate);
                }
                else
                {
                    return;
                }
            }

            if (0 == distanceComputer.Count || distanceComputer[0] == null)
            {
                return;
            }

            if (kVals.Count == 0)
            {
                kVals.Add(5);
            }
            if (rejectThresh.Count == 0)
            {
                rejectThresh.Add(0);
            }

            List <TestCollection> testUnLabels = new List <TestCollection>();
            TestCollection        testLabel    = null;

            while (iArg < args.Length - 1)
            {
                string fileName = args[iArg++];

                if (true == doTrainLabel)
                {
                    testLabel = new TestCollection(fileName);
                    if (testLabel.Faces.Count <= 0)
                    {
                        Console.WriteLine("No faces loaded from {0}", fileName);
                        return;
                    }
                }

                fileName = args[iArg++];
                TestCollection testUnLabel = new TestCollection(fileName);
                if (testUnLabel.Faces.Count <= 0)
                {
                    Console.WriteLine("No faces loaded from {0}", fileName);
                    return;
                }

                //testUnLabel.distanceComputer = EuclidianDistance;
                testUnLabel.DistanceComputer = distanceComputer[iDistanceCompute];
                if (true == doDistances)
                {
                    if (action == TestAction.Base || action == TestAction.Filtered || action == TestAction.TestPairs)
                    {
                        testUnLabel.DoDistances(testLabel);
                        testUnLabel.Sort();
                    }
                }

                testUnLabels.Add(testUnLabel);
            }

            ++iDistanceCompute;

            if (null != sw)
            {
                Console.SetOut(sw);
            }

            if (testUnLabels.Count > 0)
            {
                switch (action)
                {
                case TestAction.Base:
                    DoReport(kVals, rejectThresh, testUnLabels, verbose);
                    break;

                case TestAction.Filtered:
                    RunFiltered(kMax, kVals, testUnLabels[0], distanceComputer[iDistanceCompute], verbose);
                    DoReport(kVals, rejectThresh, testUnLabels, verbose);
                    break;

                case TestAction.TestPairs:
                    testUnLabels[0].DistanceComputer = distanceComputer[iDistanceCompute];
                    TestPairs(testUnLabels[0], -1, kVals);
                    break;

                case TestAction.GenerateTrainTriplets:
                    GenerateTrainDataTriplets(kVals, rejectThresh, testLabel, testUnLabels[0], verbose);
                    break;

                case TestAction.GenerateDepth:
                    ReportDepth(kVals, testUnLabels[0], verbose);
                    break;

                case TestAction.GenerateTrainPairs:
                    if (kVals.Count < 2)
                    {
                        throw new Exception("GenerateTrainPairs need to specify kVal for both diff and same classes");
                    }
                    GenerateTrainDataPairs(kVals[0], kVals[1], testUnLabels[0], verbose);
                    break;

                case TestAction.ReportTrainData:
                    ReportTrainData(kVals, testUnLabels[0], dataFileName);
                    break;
                }
            }

            if (null != sw)
            {
                sw.Close();
            }
        }
 public FacePairSorter(TestCollection.DistanceComputeDelegate neuralNetComparer)
 {
     _neuralNetComparer = neuralNetComparer;
 }