Skip to content
Closed
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e1678fb
added first version of hessian eigenvalues
hanslovsky Aug 3, 2016
032c525
implemented copy method
hanslovsky Aug 3, 2016
fda376d
Changes to Matrix classes and eigenvalue methods
hanslovsky Aug 3, 2016
ee213c3
copy() returning deep copy now
hanslovsky Aug 3, 2016
17dded2
added methods for passing result RandomAccesisbleIntervals
hanslovsky Aug 3, 2016
9aecda0
Added parallel version for gradient.
hanslovsky Aug 4, 2016
fbe5282
Use parallel gradient, pass number of threads
hanslovsky Aug 4, 2016
9b76979
removed time logs
hanslovsky Aug 4, 2016
b96cd39
added parrallel option to eigenvalues
hanslovsky Aug 4, 2016
00fc4a9
added author tags (PartialDerivative unclear)
hanslovsky Aug 4, 2016
a515974
Merge branch 'master' into hessian-eigenvalues
hanslovsky Aug 8, 2016
dc8c079
added documentation/comments
hanslovsky Aug 8, 2016
3074bee
Add HessianMatrixEigenValuesTest
hanslovsky Aug 8, 2016
e7b31ff
Add test for HessianMatrix (2D and 3D)
hanslovsky Aug 8, 2016
3b7b075
Remove consistency test for Hessian matrix from HessianMatrixEigenVal…
hanslovsky Aug 8, 2016
7fa9b69
Add @tpietzsch as author of PartialDerivative.java
hanslovsky Aug 8, 2016
d5871fd
Merge branch 'master' into hessian-eigenvalues
hanslovsky Aug 10, 2016
b72af5f
removed e-mails from author tags
hanslovsky Aug 10, 2016
80a2b9e
Merge remote-tracking branch 'origin/master' into hessian-eigenvalues
hanslovsky Feb 16, 2017
a7dd4d8
Simplify interfaces
hanslovsky Feb 16, 2017
5273d3c
Rename packages (more appropriately)
hanslovsky Feb 16, 2017
c925680
Use nTasks instead of nThreads
hanslovsky Feb 16, 2017
58f24bd
Add convenience methods for gaussian smoothing to Hessian matrix
hanslovsky Feb 16, 2017
a4e427c
Remove number of threads from nTasks JavaDoc
hanslovsky Feb 16, 2017
1040585
Make number of dimensions consistent
hanslovsky Feb 16, 2017
0ff0e19
Change EigenValue interface to accept ComplexType
hanslovsky Feb 22, 2017
4533ee0
Optimize EigenValue and avoid creation of object with every call to c…
hanslovsky Feb 22, 2017
1cc9f93
Remove bounds check for RealCompositeMatrix.getEntry
hanslovsky Feb 22, 2017
7f928eb
Relax interface for RealCompositeMatrix.data and EigenValue.compute f…
hanslovsky Feb 22, 2017
6489ec5
Add license header.
hanslovsky Feb 24, 2017
de07cf4
Add convenience method for scaling Hessian according to sigma.
hanslovsky Feb 24, 2017
fca300c
Add eigenvalue decomposition using ojAlgo
hanslovsky Mar 8, 2017
6d91ca2
Merge remote-tracking branch 'origin/master' into hessian-eigenvalues…
hanslovsky Apr 24, 2017
0b6b80f
Use ojAlgo instead of commons-math
hanslovsky Apr 24, 2017
ba28c27
Merge pull request #1 from hanslovsky/hessian-eigenvalues-ojalgo
hanslovsky Apr 24, 2017
f709b25
Fix formatting issues
hanslovsky Apr 24, 2017
cbfc6b5
Use gradientCentralDifference instead of gradientCentralDifference2
hanslovsky May 16, 2017
c6a1ad0
Use more concise and readable Util.min over streams.
hanslovsky May 16, 2017
1e85a8c
Make constructors with length parameters protected
hanslovsky May 16, 2017
0d6e352
Remove unused import java.util.Arrays
hanslovsky May 16, 2017
1b63be8
Specify linear representation of upper triangular Hessian in JavaDoc
hanslovsky May 16, 2017
7366220
Add JavaDoc to HessianMatrix.scaleHessianMatrix
hanslovsky May 16, 2017
c7608c4
Merge branch 'master' of github.com:imglib/imglib2-algorithm into hes…
hanslovsky May 16, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ Jean-Yves Tinevez and Michael Zinsmaier.</license.copyrightOwners>
<artifactId>trove4j</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.ojalgo</groupId>
<artifactId>ojalgo</artifactId>
<version>43.0</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
333 changes: 333 additions & 0 deletions src/main/java/net/imglib2/algorithm/gradient/HessianMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2016 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imglib2.algorithm.gradient;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.stream.IntStream;

import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.gauss3.Gauss3;
import net.imglib2.exception.IncompatibleTypeException;
import net.imglib2.outofbounds.OutOfBoundsFactory;
import net.imglib2.type.numeric.RealType;
import net.imglib2.view.IntervalView;
import net.imglib2.view.MixedTransformView;
import net.imglib2.view.Views;

/**
*
* Compute entries of n-dimensional Hessian matrix.
*
* @author Philipp Hanslovsky
*
*/
public class HessianMatrix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether it is worth adding a version that takes gaussian and gradient arguments that have appropriate size so that an OutOfBounds factory is not required. This would be useful if you want to do the Hessian computation blockwise to avoid artifacts at block boundaries. Of course, this can also be added later when it is needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not thought about that but I like the idea. I did not like the extra OufOfBounds so we should keep that in our minds. I have only one concern which actually might be a non-issue: If you already have the gradients pre-computed, you can just call the overload takes only the gradients as input. If you do not have them pre-computed, you will probably want to re-use them as features elsewhere and you might want to have them at the same size as the input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I fully agree

{

/**
* @param source
* n-dimensional input {@link RandomAccessibleInterval}
* @param gaussian
* n-dimensional output parameter
* {@link RandomAccessibleInterval}
* @param gradient
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* the gradients along all axes of the smoothed source (size of
* last dimension is n)
* @param hessianMatrix
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* all second partial derivatives (size of last dimension is n *
* ( n + 1 ) / 2)
* @param outOfBounds
* {@link OutOfBoundsFactory} that specifies how out of bound
* pixels of intermediate results should be handled (necessary
* for gradient computation).
* @param sigma
* Scale for Gaussian smoothing.
*
* @return Hessian matrix that was passed as output parameter.
* @throws IncompatibleTypeException
*/
public static < T extends RealType< T >, U extends RealType< U > > RandomAccessibleInterval< U > calculateMatrix(
final RandomAccessible< T > source,
final RandomAccessibleInterval< U > gaussian,
final RandomAccessibleInterval< U > gradient,
final RandomAccessibleInterval< U > hessianMatrix,
final OutOfBoundsFactory< U, ? super RandomAccessibleInterval< U > > outOfBounds,
final double... sigma ) throws IncompatibleTypeException
{

if ( sigma.length == 1 )
Gauss3.gauss( sigma[ 0 ], source, gaussian );
else
Gauss3.gauss( sigma, source, gaussian );
return calculateMatrix( Views.extend( gaussian, outOfBounds ), gradient, hessianMatrix, outOfBounds );
}

/**
* @param source
* n-dimensional pre-smoothed {@link RandomAccessible}. It is the
* callers responsibility to smooth the input at the desired
* scales.
* @param gradient
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* the gradients along all axes of the smoothed source (size of
* last dimension is n)
* @param hessianMatrix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a javadoc description of the upper triangular layout of the hessianMatrix (can be adapted from TensorEigenValues javadoc)?

* n+1-dimensional {@link RandomAccessibleInterval} for storing
* all second partial derivatives (size of last dimension is n *
* ( n + 1 ) / 2)
* @param outOfBounds
* {@link OutOfBoundsFactory} that specifies how out of bound
* pixels of intermediate results should be handled (necessary
* for gradient computation).
*
* @return Hessian matrix that was passed as output parameter.
*/
public static < T extends RealType< T > > RandomAccessibleInterval< T > calculateMatrix(
final RandomAccessible< T > source,
final RandomAccessibleInterval< T > gradient,
final RandomAccessibleInterval< T > hessianMatrix,
final OutOfBoundsFactory< T, ? super RandomAccessibleInterval< T > > outOfBounds )
{

final int nDim = gradient.numDimensions() - 1;

for ( long d = 0; d < nDim; ++d )
PartialDerivative.gradientCentralDifference( source, Views.hyperSlice( gradient, nDim, d ), ( int ) d );

return calculateMatrix( Views.extend( gradient, outOfBounds ), hessianMatrix );
}

/**
*
* @param gradient
* n+1-dimensional {@link RandomAccessible} containing the
* gradients along all axes of the smoothed source (size of last
* dimension is n)
* @param hessianMatrix
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* all second partial derivatives (size of last dimension is n *
* ( n + 1 ) / 2)
*
* @return Hessian matrix that was passed as output parameter.
*/
public static < T extends RealType< T > > RandomAccessibleInterval< T > calculateMatrix(
final RandomAccessible< T > gradients,
final RandomAccessibleInterval< T > hessianMatrix )
{

final int nDim = gradients.numDimensions() - 1;

long count = 0;
for ( long d1 = 0; d1 < nDim; ++d1 )
{
final MixedTransformView< T > hs1 = Views.hyperSlice( gradients, nDim, d1 );
for ( long d2 = d1; d2 < nDim; ++d2 )
{
final IntervalView< T > hs2 = Views.hyperSlice( hessianMatrix, nDim, count );
PartialDerivative.gradientCentralDifference( hs1, hs2, ( int ) d2 );
++count;
}
}
return hessianMatrix;
}

// parallel

/**
* @param source
* n-dimensional input {@link RandomAccessibleInterval}
* @param gaussian
* n-dimensional output parameter
* {@link RandomAccessibleInterval}
* @param gradient
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* the gradients along all axes of the smoothed source (size of
* last dimension is n)
* @param hessianMatrix
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* all second partial derivatives (size of last dimension is n *
* ( n + 1 ) / 2)
* @param outOfBounds
* {@link OutOfBoundsFactory} that specifies how out of bound
* pixels of intermediate results should be handled (necessary
* for gradient computation).
* @param nTasks
* Number of tasks used for parallel computation of eigenvalues.
* @param es
* {@link ExecutorService} providing workers for parallel
* computation. Service is managed (created, shutdown) by caller.
* @param sigma
* Scale for Gaussian smoothing.
*
* @return Hessian matrix that was passed as output parameter.
* @throws IncompatibleTypeException
* @throws ExecutionException
* @throws InterruptedException
*/
public static < T extends RealType< T >, U extends RealType< U > > RandomAccessibleInterval< U > calculateMatrix(
final RandomAccessible< T > source,
final RandomAccessibleInterval< U > gaussian,
final RandomAccessibleInterval< U > gradient,
final RandomAccessibleInterval< U > hessianMatrix,
final OutOfBoundsFactory< U, ? super RandomAccessibleInterval< U > > outOfBounds,
final int nTasks,
final ExecutorService es,
final double... sigma ) throws IncompatibleTypeException, InterruptedException, ExecutionException
{

if ( sigma.length == 1 )
Gauss3.gauss( IntStream.range( 0, source.numDimensions() ).mapToDouble( i -> sigma[ 0 ] ).toArray(), source, gaussian, es );
else
Gauss3.gauss( sigma, source, gaussian, es );
return calculateMatrix( Views.extend( gaussian, outOfBounds ), gradient, hessianMatrix, outOfBounds, nTasks, es );
}

/**
* @param source
* n-dimensional pre-smoothed {@link RandomAccessible}. It is the
* callers responsibility to smooth the input at the desired
* scales.
* @param gradient
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* the gradients along all axes of the smoothed source (size of
* last dimension is n)
* @param hessianMatrix
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* all second partial derivatives (size of last dimension is n *
* ( n + 1 ) / 2)
* @param outOfBounds
* {@link OutOfBoundsFactory} that specifies how out of bound
* pixels of intermediate results should be handled (necessary
* for gradient computation).
* @param nTasks
* Number of tasks used for parallel computation of eigenvalues.
* @param es
* {@link ExecutorService} providing workers for parallel
* computation. Service is managed (created, shutdown) by caller.
*
* @return Hessian matrix that was passed as output parameter.
* @throws IncompatibleTypeException
* @throws ExecutionException
* @throws InterruptedException
*/
public static < T extends RealType< T > > RandomAccessibleInterval< T > calculateMatrix(
final RandomAccessible< T > source,
final RandomAccessibleInterval< T > gradient,
final RandomAccessibleInterval< T > hessianMatrix,
final OutOfBoundsFactory< T, ? super RandomAccessibleInterval< T > > outOfBounds,
final int nTasks,
final ExecutorService es ) throws IncompatibleTypeException, InterruptedException, ExecutionException
{

final int nDim = gradient.numDimensions() - 1;

for ( long d = 0; d < nDim; ++d )
PartialDerivative.gradientCentralDifferenceParallel( source, Views.hyperSlice( gradient, nDim, d ), ( int ) d, nTasks, es );

return calculateMatrix( Views.extend( gradient, outOfBounds ), hessianMatrix, nTasks, es );
}

/**
*
* @param gradient
* n+1-dimensional {@link RandomAccessible} containing the
* gradients along all axes of the smoothed source (size of last
* dimension is n)
* @param hessianMatrix
* n+1-dimensional {@link RandomAccessibleInterval} for storing
* all second partial derivatives (size of last dimension is n *
* ( n + 1 ) / 2)
* @param nTasks
* Number of tasks used for parallel computation of eigenvalues.
* @param es
* {@link ExecutorService} providing workers for parallel
* computation. Service is managed (created, shutdown) by caller.
*
* @return Hessian matrix that was passed as output parameter.
* @throws IncompatibleTypeException
* @throws ExecutionException
* @throws InterruptedException
*/
public static < T extends RealType< T > > RandomAccessibleInterval< T > calculateMatrix(
final RandomAccessible< T > gradient,
final RandomAccessibleInterval< T > hessianMatrix,
final int nTasks,
final ExecutorService es ) throws IncompatibleTypeException, InterruptedException, ExecutionException
{

final int nDim = gradient.numDimensions() - 1;

long count = 0;
for ( long d1 = 0; d1 < nDim; ++d1 )
{
final MixedTransformView< T > hs1 = Views.hyperSlice( gradient, nDim, d1 );
for ( long d2 = d1; d2 < nDim; ++d2 )
{
final IntervalView< T > hs2 = Views.hyperSlice( hessianMatrix, nDim, count );
PartialDerivative.gradientCentralDifferenceParallel( hs1, hs2, ( int ) d2, nTasks, es );
++count;
}
}
return hessianMatrix;
}

public static < T extends RealType< T > > IntervalView< T > scaleHessianMatrix( final RandomAccessibleInterval< T > hessian, final double[] sigma )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add javadoc? It's not obvious what this method does

{

assert sigma.length == hessian.numDimensions() - 1;
assert sigma.length * ( sigma.length + 1 ) / 2 == hessian.dimension( sigma.length );
final int maxD = sigma.length;

final double minSigma = Arrays.stream( sigma ).reduce( Double.MAX_VALUE, ( d1, d2 ) -> Math.min( d1, d2 ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final double minSigma = Util.min( sigma ) would be more readable. (net.imglib2.util.Util)

final double minSigmaSq = minSigma * minSigma;
final double[] sigmaSquared = new double[ sigma.length * ( sigma.length + 1 ) / 2 ];
for ( int i1 = 0, k = 0; i1 < sigma.length; ++i1 )
for ( int i2 = i1; i2 < sigma.length; ++i2, ++k )
sigmaSquared[ k ] = sigma[ i1 ] * sigma[ i2 ] / minSigmaSq;

final ScaleAsFunctionOfPosition< T > scaledMatrix = new ScaleAsFunctionOfPosition<>( hessian, l -> {
return sigmaSquared[ l.getIntPosition( maxD ) ];
} );

return Views.interval( scaledMatrix, hessian );
}


}
Loading