Parameters for dlib::find_min_bobyqa - dlib

I'm working on the C++ version of Matt Zucker's Page dewarping. So far everything works fine, but I have a problem with optimization. In line 748 of Github repo Matt uses optimize function from Scipy. My C++ equivalent is find_min_bobyqa from dlib.net. The code is:
auto f = [&](const column_vector& ppts) { return objective( dstpoints, ppts, keypoint_index); };
dlib::find_min_bobyqa(f,
params,
2 * params.nr() + 1, // npt - number of interpolation points: x.size() + 2 <= npt && npt <= (x.size()+1)*(x.size()+2)/2
dlib::uniform_matrix<double>(params.nr(), 1, -2), // lower bound constraint
dlib::uniform_matrix<double>(params.nr(), 1, 2), // upper bound constraint
1, // initial trust region radius
1e-5, // stopping trust region radius
4000 // max number of objective function evaluations
);
In my concrete example params is a dlib::column_vector with double values and length = 189. Every element of params is less than 2.0 and greater than -2.0. Function objective() returns double value and "alone" it works properly because I get the same value as in the Python version. But after running fin_min_bobyqa function I usually get the message:
Terminate called after throwing an instance of 'dlib:bobyqa_failure', return from BOBYQA because the objective function has been called max_f_evals times.
I set max_f_evals to quite big value to see if it optimizes at all, but it doesn't. I did some tweaking with parameters but without good results. How should I set the parameters of find_min_bobyqa to get the right solution?

I am very interested in this issue as well. Zucker's work, with very minor tweaks, is ideal for straightening sheet music images, and I was looking for ways to implement it in a mobile platform when I came across your question.
My research so far suggests that BOBYQA is not the equivalent of Powell's method in scipy. BOBYQA is constrained, and the one in scipy is not.
See these links for more information, and a possible way to compile the right supporting library - I would try UOBYQA or NEWUOA.
https://github.com/jacobwilliams/PowellOpt
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#rdd2e1855725e-3
(See the Notes section)
EDIT: see C version here:
https://github.com/emmt/Algorithms/tree/master/newuoa
I wanted to post this as a comment, but I don't have enough points for that.
I am very interested in your progress. If you're willing, please keep me posted.

I finally solved this problem. I used PRAXIS library, because it doesn't need derivative information and is fast.
I modified the code a little to my needs and now it is faster around few seconds than original version written in Python.

Related

Using fxrand() for p5 project?

I used the p5 editor to build an nft, and I'm working on getting it working in the fxhash sandbox. Using p5's random() function worked great when I uploaded my project to the sandbox, but quickly realized I needed to implement the fxrand() function to ensure that each individual iteration is the same when refreshing with the same hash.
Simply replacing all instances of the p5 random() function with fxrand() did not work, and I'm assuming because fxrand() simply generates a random number, whereas p5's random() function can be used in other ways (ie; random(-50, 50)).
How do I need to incorporate the fxrand() function into my project in a way that still works the same way as p5's random() function?
You may have already figured this out, and there's probably far better solutions (I'm very new to this), but here is what I figured out.
If I need a number from 0 to 9 then I can use this:
let randChoose1 = Math.floor(fxrand() * 10;
Anywhere I need to call that number I can simply use randChoose1 in place. For example, if I have an array named "bg" with 10 entries in the array, and I want to choose something from the array I can do this:
let randoBg = bg[randChoose1];
Maybe that's an image that I want to center on the canvas, I can call it with:
image(randoBg, width / 2, height / 2);
If I have 23 items in the array then I just need to declare randChoose1 with:
let randChoose1 = Math.floor(fxrand() * 23);
If you want to be able to have negative numbers be chosen, such as your example of a range from -50 to 50, it's just a matter of multiplying by rough total range you want and then subtracting half that.
let randChoose1 = Math.floor(fxrand() * 101 - 50;
In this case, randChoose1 will give you that range of results from -50 to 50. You have to multiply by 101 in order to make it possible for Math.floor to deliver 100 since it always rounds down.
If you found a better solution I'd love to hear it! This is something I'm struggling with as well, and my total experience with p5.js is less than a week at this point.
If you use randomSeed(int(fxrand()*987654321)) at the beginning of the setup function every time you call to the random function it will depend on fxrand

deterministic dithering with libsoxr

I have an issue with some unit test code which is giving different results for every execution.
I tracked it back to libsoxr (0.1.3) and discovered that is its down to the dithering option:
That is, if soxr_create() is invoked with:
soxr_io_spec_t soxIoSpec = soxr_io_spec(SOXR_INT16_I, SOXR_INT16_I);
sxIoSpec.flags |= SOXR_NO_DITHER;
The output of soxr_process() is deterministic.
But without adding the SOXR_NO_DITHER flag the output is slightly different for each execution.
There is another thing about the library which surprises me here.
soxr_oneshot() does not suffer from this problem (the non-determinism).
What is going on here?
Looking into the code I see that in soxr.c the dither uses a pseudo random number generator
but the seed is an implementation detail generated from the time by:
p->seed = (unsigned long)time(0) ^ (unsigned long)(size_t)p;
It does not seem to be exposed by the library thus preventing you from setting a particular seed which you could otherwise do tog et the same result each time the test is run.
I have suggested a minor enhanced to the API like the below to facilitate this,
though someone with more knowledge of the library may be able to suggest a better way.
In Soxr.h add:
typedef unsigned long soxr_seed_t;
// set or retrieve the random seed used by the dithering function
void soxr_setseed(soxr_t resampler, soxr_seed_t new_seed);
soxr_seed_t soxr_getseed(soxr_t* resampler);
In Soxr.c add:
void soxr_setseed(soxr_t resampler, soxr_seed_t new_seed)
{
resampler->seed = new_seed;
}
soxr_seed_t soxr_getseed(soxr_t resampler)
{
return resampler->seed;
}
One thing about the library which still surprises me here is that
soxr_oneshot() does not suffer from this problem (the non-determinism).
I can’t see how the seed is fixed or SOXR_NO_DITHER set by the internal call to soxr_create().
I have obviously missed something here which someone with more knowledge of the library may be able to explain.

Ruby SketchUp ... add a measurment

still learning about Ruby + Sketchup!
Today,I would like to add a measurement (good english word ?) as I can do manually with the 'cotation' (french version) tool when I click to point then drag the measure text.
Can't find that in the docs to do with Ruby and API ...
Thanks for your help
You are probably looking for the Sketchup::Entities::add_dimension_linear method.
http://ruby.sketchup.com/Sketchup/Entities.html#add_dimension_linear-instance_method
Assuming a and b below are edges
voffset = [-20, 0, 0]
Sketchup.active_model.entities.add_dimension_linear(a.start, b.start, voffset)
The value of voffset controls not just how far offset the dimension is, but also the axis along which the measurement is made. You may need to experiment with different values to get a feeling for how that determination is done. As with many things in SketchUp, it often guesses (or 'infers') at what you want.

Swift array access triggers EXC_BREAKPOINT

Here is my code (largeAsteroids.count is never 0):
var largeAsteroids=[[SKTexture]]()
func randomLargeAsteroidTextures()->Array<SKTexture>{
let i=Int(arc4random())%largeAsteroids.count
return largeAsteroids[i]// this line triggers EXC_BREAKPOINT
}
When I execute my code, I receive no errors but I get a EXC_BREAKPOINT. I ensured there wasn't any breakpoint and at index i there was a valid object.
First I changed SKTexture to AnyObject, it didn't help.
Then I tried to use NSMutableArray instead of swift array, problem still exist:
var largeAsteroids=NSMutableArray()
func randomLargeAsteroidTextures()->AnyObject{
let i=Int(arc4random())%largeAsteroids.count
return largeAsteroids.objectAtIndex(i) // this line triggers EXC_BREAKPOINT
}
update:
Problem solved,
replace:
let i=Int(arc4random())%largeAsteroids.count
by:
let i=Int(arc4random_uniform(UInt32(largeAsteroids.count)))
Thanks for Matt's solution:
You should probably be using arc4random_uniform. You'll get modulo bias from your current implementation. – Matt Gibson
You were running on a 32-bit target, yes? On a 32-bit target (e.g. iPhone 4), Swift Ints are 32-bits, and signed. However, on any platform, arc4random() returns a 32-bit unsigned integer.
Because of this conflict, and your conversion to Int, Int(arc4random()), sometimes—in fact, half the time, all else being equal—your number was negative, giving you a negative array index, and causing your problem (though I get EXC_BAD_INSTRUCTION, as I'd expect, when I reproduce the problem; presumably you have a breakpoint set for exceptions?)
My suggestion, to use arc4random_uniform, should work fine as long as the count of your asteroids is never more than Int.max on a 32-bit platform, which is presumably quite unlikely, unless you're giving your gamer a really hard time. It will also avoid modulo bias in the random generation, so your resulting random numbers will be more uniformly distributed than in your original solution.

term does not evaluate to a function taking 1 arguments

Please have a look at the following OpenCV code
Mat *curent;
current = new Mat();
cv::Rect bRect = cv::boundingRect(Mat(*points).reshape(2));
Mat roi = *current(bRect);
Here, I am trying to get a ROI to the Mat called roi. But whenever I try to get execute the last line of the above code I get the error term does not evaluate to a function taking 1 arguments. I have followed the same technique of getting an ROI without pointers number of times before in C++ and they worked. I guess the issue is with pointer current ? current must be a pointer because local variable slowed the application in an unbelievable way.
So, how can I solve this issue and get the ROI ?
please, throw out those pointers!
you're going to wreck havoc on the internal Mat refcounts, produce undefined behaviour and memleaks
"local variable slowed the application in an unbelievable way."
really, you think, copying a 58 byte struct is the reason ? i just don't believe you.
well i'll give you a hint, anyway - the ( ) operator has a higher precedence than the * operator.

Resources