コード例 #1
0
ファイル: Model.cs プロジェクト: exitmouse/drfcsharp
        public Classification ICMInfer(ImageData test_input)
        {
            Classification curr_classification = LogisticInfer(test_input); //Muahaha better initialization. I don't think this is hacky.
            //for(int x = 0; x < ImageData.x_sites; x++) for(int y = 0; y < ImageData.y_sites; y++) curr_classification[x,y] = Label.OFF;
            bool converged = false;
            int loopcount = 0;
            while(!converged && loopcount < 300)
            {
                loopcount++;
                int changecount = 0;
                converged = true;
                for(int x = 0; x < test_input.XSites; x++) for(int y = 0; y < test_input.YSites; y++)
                {
                    Label old = curr_classification[x,y];
                    //We have prob 1 vs prob 0. Prob n \propto exp(A + sum over neighbors of I calculated at n)
                    //So we can just calculate A + sum over neighbors of I for each labeling of the site, and
                    //assign to the site whichever is higher.
                    var sitefeatures = Transformer.Transform(test_input[x,y]);
                    if(x == 6 && y == 10)
                    {
                        Console.WriteLine("Components of the dot product w*sitefeatures:");
                        for(int i = 0; i < sitefeatures.Count; i++)
                        {
                            Console.WriteLine("{0}th component: {1}", i, W[i]*sitefeatures[i]);
                        }

                    }
                    double on_association = MathWrapper.Log(MathWrapper.Sigma(W.DotProduct(sitefeatures)));
                    double off_association = MathWrapper.Log(MathWrapper.Sigma(-1 * W.DotProduct(sitefeatures)));
                    double on_interaction = 0d;
                    double off_interaction = 0d;

                    foreach(Tuple<int,int> t in test_input.GetNeighbors(x,y))
                    {
                        DenseVector mu;
                        if(ImageData.IsEarlier(x,y,t.Item1,t.Item2))mu = Crosser.Cross(test_input[x,y],test_input[t.Item1,t.Item2]);
                        else mu = Crosser.Cross(test_input[t.Item1,t.Item2], test_input[x,y]);
                        //Console.WriteLine("Magnitude of Interaction: {0}",v.DotProduct(mu));
                        if(curr_classification[t.Item1,t.Item2] == Label.ON)
                        {
                            on_interaction += V.DotProduct(mu);
                            off_interaction -= V.DotProduct(mu);
                        }
                        else
                        {
                            on_interaction -= V.DotProduct(mu);
                            off_interaction += V.DotProduct(mu);
                        }
                    }

                    if(on_association + on_interaction > off_association + off_interaction)
                    {
                        /*Console.WriteLine("On Association: {0}",on_association);
                        Console.WriteLine("Off Association: {0}",off_association);*/
                        curr_classification[x,y] = Label.ON;
                    }
                    else
                    {
                        curr_classification[x,y] = Label.OFF;
                    }
                    if(curr_classification[x,y] != old)
                    {
                        converged = false;
                        changecount += 1;
                    }
                }
                Console.WriteLine("Number of changes in this round of ICM: {0}",changecount);
            }
            return curr_classification;
        }