A new forward_distmat
method to accelerate distance matrix computing among images.
#101
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Add another forward function
forward_distmat
to compute the distance matrix with one or two stacks of images.Rationale
The current
forward
method of LPIPS supports computing the distance between two images(A,B)
, or between one image to a batch of images(As,Bs)
or the distance between corresponding pairs in two batches(As,Bs)
.Because of this, computing the distance matrix cannot be directly performed using the current
forward function
It needs to be computed row by row, for example given a
imgtsrs
of shape[B,C,H,W]
A simple fix to accelerate this is to move this loop into the
forward
function. Then it can reuse the features computed for images, so it largely accelerates the speed of computing a large distance matrix among a set of images.Memory usage.
We choose to compute the matrix row by row, such that the memory footprint shall be still linear in the number of images instead of quadratic.
TODO
It could be extended to compute a few rows in a batch. The batch size needs to be chosen w.r.t. memory.
Currently, it's fast enough for most of my works, but there is space for further optimization.
If there is interest in more efficiency I can help implement more.