求解器适合2D高斯分布 [英] Solver to fit a 2D gaussian distribution

查看:82
本文介绍了求解器适合2D高斯分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用C#和Solver来拟合2D高斯。使用具有各种sigmas,强度和中心的模拟数据(无噪音),它工作得很好。然而,对于真实数据,即使所有测试数据都是大约相同质量和大约相同的图像,它仅在大约50%的时间内工作。 x& s的相同西格玛年。我可以提交代码,以便有更多经验的人可以看到我做错了什么。



图像大约是100x100,分辨率为5 mm,两个x的sigmas大约为7mm &安培; y。



当失败时,它失败,sigmas值和强度都接近0.0。



这是一个如此简单的问题,我不认为Solver无法处理它,数据根本就没那么吵。它必须是一些微妙的东西,我没有正确构建模型。



如果有人可以提供帮助我会非常感激。



谢谢,



Daniel



这是主要代码

Hi,

I am using C# and the Solver to fit a 2D Gaussian. Using simulated data (no noise) with various sigmas, intensities and center, it was working perfectly. However, with real data, it works only about 50% of time even though all the test data are images of about the same quality and approx. same sigmas for both x & y. Can I submit the code so someone who has more experience can see what I did wrong.

Images are about 100x100 with 5 mm resolution and sigmas of about 7mm for both x & y.

When failed, it failed with values of sigmas and intensity both close to 0.0.

It is such a simple problem, I don't think the Solver can't handle it and the data are not that noisy at all. It must be something subtle that I didn't construct the model properly.

I would greatly appreciate if someone can help.

Thanks,

Daniel

Here is the main code

var solver = SolverContext.GetContext();
            solver.ClearModel();
            var model = solver.CreateModel();
                 
            var decisionsA = Spot_Params.Spot_ParamsA.Select(
                it => new Decision(Domain.RealNonnegative, it.Key));
            model.AddDecisions(decisionsA.ToArray());
            var decisionsB = Spot_Params.Spot_ParamsB.Select(
                it => new Decision(Domain.Real, it.Key));
            model.AddDecisions(decisionsB.ToArray());
          
            var objective = new SumTermBuilder(gaussMatrix.Data_1D.Count());
            var sx = model.Decisions.ElementAt(0);           
            var sy = model.Decisions.ElementAt(1);           
            var sf = model.Decisions.ElementAt(2);            
            var cx = model.Decisions.ElementAt(3);            
            var cy = model.Decisions.ElementAt(4);           

            var sx_sq_2_inv = 1.0 / (sx * sx * 2.0);
            var sy_sq_2_inv = 1.0 / (sy * sy * 2.0);
                      
            int n = 0;
            for (int j = 0; j < gaussMatrix.Nrow; j++)
            {
                var dy = gaussMatrix.Ypos[j] - cy;

                for (int i = 0; i < gaussMatrix.Ncol; i++)
                {                              
                    var dx = gaussMatrix.Xpos[i] - cx;
                    var v = (dx * dx) * sx_sq_2_inv + (dy * dy) * sy_sq_2_inv;                  
                    var value = sf * Model.Exp(-v);
                    var diff = gaussMatrix.Data_1D[n++] - value;

                    objective.Add(diff * diff);
                }
            }

            model.AddGoal("OptimizedSpotParams", GoalKind.Minimize, objective.ToTerm());
                      
            // Extract fitted parameters
          
            var spot_params = fittedParams.Spot_Params;
            Solution solution;
            solution = await Task.Run(() => solver.Solve());





什么我试过了:



已审核的代码和输入数据。看不到任何明显错误



What I have tried:

Reviewed code and input data. Couldn't see anything obviously wrong

推荐答案

引用:

失败时,它失败,西格玛值和强度都接近0.0。

When failed, it failed with values of sigmas and intensity both close to 0.0.





继续测试并找出它失败的位置; 接近只适用于马鞋。



Keep testing and find out "where" it fails; "close to" is only good in horse shoes.


引用:

这是一个简单的问题,我不认为Solver无法处理它,数据根本就没那么吵。它必须是一些微妙的东西,我没有正确构建模型。

It is such a simple problem, I don't think the Solver can't handle it and the data are not that noisy at all. It must be something subtle that I didn't construct the model properly.



您的代码根据数据工作,如果没有这些数据,我们无法找到错误。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你显示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



在Visual Studio中调试C#代码 - YouTube [ ^ ]



调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


your code work depending on data, we have no way to find what is wrong without that data.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


感谢您的回复。

我已通过调试器。但优化代码没有显示实际运行,除了没有实际值定义的'变量',所以没有办法(我知道)找出优化过程中发生的事情。



数据和代码非常简单。我可以提供一个zip文件给别人来试试吗?



我有数据可行,数据失败。



谢谢,



Daniel
Thanks for the response.
I have gone through the debugger. But the optimizing code doesn't show you the actual run except the 'variables' defined without actual values so there is no way (as I know of) to figure out what is happening during optimization.

The data and code are very simple. Can I provide a zip file to someone to try it out?

I have data that works, and data that fails.

Thanks,

Daniel


这篇关于求解器适合2D高斯分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆