Optimization of FIT function in a FOR loop
Ältere Kommentare anzeigen
My current project work is based on processing tiff images. I have x images each of size m x n pixels.
I am using Matlab FIT function to fit a customised equation using data from all x images at each pixel (mxn). From the fit, three parameters need to be calculated.
I am using 2 for loop to calculate the fit for each pixel. The program is extremely slow (> 6 hours) during execution of the 2 for loops even with parallel processing. Preallocation is already included in the code.
Is there a way to call the FIT function just once and it executes the fitting for all pixels?
The relevant code is attached here, fitt() defines all the options for fit V1 & S1 are input parameters, X1 Y1 is dimensions of the images.
parfor i = 1:X1
[ft1, opts1] = fitt();
for j = 1:Y1
[result1, gof1] = fit(V1(:,i,j),S1(:,i,j),ft1,opts1);
A1(i,j)=result1.C3;
A2(i,j)=result1.C1;
A3(i,j)=result1.C2;
end
end
I am using Matlab version R2012a on Windows Vista OS.
Thanks
Antworten (1)
Sean de Wolski
am 16 Aug. 2013
1 Stimme
A few questions and thoughts:
- What are you actually fitting and how big are your images?
- Also, have you tried LSQCURVEFIT in the Optimization Toolbox? I think you might see a speedup with it.
- Another thing to consider is how much variation do you expect in the coefficients of adjacent or near adjacent pixels? If the variation is not expected to be much, use the results from the previous iteration as your initial guess in the next iteration. This will hopefully give the optimizer a good starting point so that it can converge with fewer iterations.
- Do you really need to do this at each pixel? Downsampling by a factor of two in each dimension will get you a 4x speedup pretty effortlessly.
8 Kommentare
Pooja C
am 19 Aug. 2013
Sean de Wolski
am 19 Aug. 2013
I haven't timed it but expect it to be faster as it is just a solver, it does not compute other things as well.
Pooja C
am 23 Aug. 2013
Sean de Wolski
am 23 Aug. 2013
Hi Pooja,
Glad to hear lsqcurvefit sped it up. You are certainly right that PARFOR requires independent iterations. However, there are approaches you could take to get around this. Perhaps a parfor loop with a regular for loop nested inside of it. You could also do a coarse search, say look at every 3rd or 4th pixel in each dimension. Save these results, and then use that as a starting point for all of the neighbor pixels in a second round of parfor. This would alleviate the dependency between iterations.
It might help further to use LSQCURVEFIT with a user-supplied Jacobian calculation, if this is not being done already,
options=optimoptions(@lsqcurvefit,'Jacobian','on')
It sounds like this could cut down on the number of exp() operations by at least a few factors, and might also accelerate convergence.
Pooja C
am 27 Aug. 2013
Hi Matt J, The Jacobian showed little improvement in time reduction.
It's more than just turning Jacobian on. There is code optimization to be done. You have to recycle the exponential expressions used in the objective function calculation when computing the Jacobian.
Pooja C
am 29 Aug. 2013
Kategorien
Mehr zu Big Data Processing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!