Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Translate affine transform generator #2297

Merged
merged 10 commits into from
Sep 29, 2020

Conversation

jantonguirao
Copy link
Contributor

@jantonguirao jantonguirao commented Sep 24, 2020

Signed-off-by: Joaquin Anton janton@nvidia.com

Why we need this PR?

  • It adds a new feature need to be able to produce affine transform matrices that can be later used in operators such as WarpAffine, CoordsTransform, etc

What happened in this PR?

Fill relevant points, put NA otherwise. Replace anything inside []

  • What solution was applied:
    Introduced a TransformBaseOp that defines most of the logic necessary to implement the different transformations. TransformBaseOp uses the curiously recurring template pattern (CRTP)
    Added the implementation of TranslateTransform, including tests
  • Affected modules and functionalities:
    New operator
  • Key points relevant for the review:
    TransformBaseOp and TranslateTransform
  • Validation and testing:
    Python tests added
  • Documentation (including examples):
    Doc str

JIRA TASK: [DALI-1626]


template <typename T>
void RunImplTyped(workspace_t<Backend> &ws, dims<>) {
DALI_FAIL(make_string("Unsupported number of dimensions ", ndim_)); // NOLINT
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
DALI_FAIL(make_string("Unsupported number of dimensions ", ndim_)); // NOLINT
DALI_FAIL(make_string("Unsupported number of dimensions ", ndim_));

leftover from VALUE_SWITCH, I guess...

Comment on lines 73 to 76
for (int i = 0; i < nsamples_; i++) {
DALI_ENFORCE(shape[i] == shape[0],
make_string("Samples are expected to have the same dimensionality. Got: ", shape));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (int i = 0; i < nsamples_; i++) {
DALI_ENFORCE(shape[i] == shape[0],
make_string("Samples are expected to have the same dimensionality. Got: ", shape));
}
DALI_ENFORCE(is_uniform(shape), "All matrices must have the same shape.");

...and I'd move this check higher.

} else {
assert(ndim == offset_.size());
auto &matrix = matrices[0];
matrix = affine_mat_t<T, ndim>::identity();
Copy link
Contributor

@mzient mzient Sep 25, 2020

Choose a reason for hiding this comment

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

Suggested change
matrix = affine_mat_t<T, ndim>::identity();
matrix = 1;

much shorter :)

}
} else {
assert(ndim == offset_.size());
auto &matrix = matrices[0];
Copy link
Contributor

@mzient mzient Sep 25, 2020

Choose a reason for hiding this comment

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

Either do it once or query for offset argument each time. Now you keep rewriting the matrices with a value that can't change at run-time.


DALI_SCHEMA(TranslateTransform)
.DocStr(R"(TranslateTransform)")
.AddArg("offset", "Translation vector", DALI_FLOAT_VEC, true)
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should rename it to "T" to aligned with CoordTransform

Suggested change
.AddArg("offset", "Translation vector", DALI_FLOAT_VEC, true)
.AddArg("T", "Translation vector", DALI_FLOAT_VEC, true)

}

auto &out = ws.template OutputRef<Backend>(0);
out.SetLayout(""); // TODO(janton): Decide what layout we want for transforms
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can live with no layout for plain matrices - unless we want to somehow stress that they're row-major - then maybe "rc" (row col).

const TransformImpl &This() const noexcept { return static_cast<const TransformImpl&>(*this); }

protected:
void CheckInputDimensionality(const workspace_t<CPUBackend> &ws) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd avoid the name "dimensionality" - especially as a standalone word. Here I'd use

Suggested change
void CheckInputDimensionality(const workspace_t<CPUBackend> &ws) {
void CheckInputShape(const workspace_t<CPUBackend> &ws) {

Comment on lines 63 to 64
"Unexpected number of dimensions: expected ", ndim_,
" dimensions but the input transform has ", in_t_ndim));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Unexpected number of dimensions: expected ", ndim_,
" dimensions but the input transform has ", in_t_ndim));
"The input describes a ", in_t_dim, "D transform but other arguments suggest a ",
ndim_, "D transform"));

int nsamples_ = -1;
bool has_input_ = false;

std::vector<uint8_t> matrices_data_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Grammar Nazi to the rescue:

Suggested change
std::vector<uint8_t> matrices_data_;
std::vector<uint8_t> matrix_data_;

or, if you insist on plural:

Suggested change
std::vector<uint8_t> matrices_data_;
std::vector<uint8_t> raw_matrices_;

BTW - if you're type-punning these, why not a Tensor?

Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao jantonguirao changed the title [WIP] Affine transform generators Add Translate Affine transform generator Sep 29, 2020
@jantonguirao jantonguirao changed the title Add Translate Affine transform generator Add Translate affine transform generator Sep 29, 2020
#include "dali/pipeline/data/views.h"

namespace dali {

DALI_SCHEMA(TranslateTransform)
.DocStr(R"(TranslateTransform)")
.AddArg("offset", "Translation vector", DALI_FLOAT_VEC, true)
.DocStr(R"(Produces a translation affine transform.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.DocStr(R"(Produces a translation affine transform.
.DocStr(R"(Produces a translation affine transform matrix.

.AddArg("offset", "Translation vector", DALI_FLOAT_VEC, true)
.DocStr(R"(Produces a translation affine transform.

If another transform is passed as an input, the operator will combine the two transforms by applying
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
If another transform is passed as an input, the operator will combine the two transforms by applying
If another transform matrix is passed as an input, the operator will combine the two matrices by applying

translation to the transform provided.

.. note::
The output of this operator can be fed directly to ``MT`` argument of ``CoordTransform`` operator.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use it in Warp operator for images as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know, WarpAffine operator expects the inverse of the transform, so it can't be directly fed into it until WarpAffine implements the flag to accept inverse transforms. @mzient Correct?

Copy link
Contributor

@mzient mzient Sep 29, 2020

Choose a reason for hiding this comment

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

For translation, we'd just need to use the negated translation vector. Or, if you don't have point cloud to go with your image, you can construct the dst-to-src transform directly.

)")
.AddArg(
"offset",
"Translation vector. The number of dimensions of the transform is inferred from this argument",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Translation vector. The number of dimensions of the transform is inferred from this argument",
R"code(Translation vector.
The number of dimensions of the transform is inferred from this argument)code",

@jantonguirao jantonguirao force-pushed the affine_transform_generators branch 2 times, most recently from a86b681 to 0e8b347 Compare September 29, 2020 09:09
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao jantonguirao force-pushed the affine_transform_generators branch 3 times, most recently from adf06b9 to ecc3501 Compare September 29, 2020 11:16
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao
Copy link
Contributor Author

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1662998]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1662998]: BUILD FAILED

Signed-off-by: Joaquin Anton <janton@nvidia.com>
@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1663069]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1663069]: BUILD FAILED

Signed-off-by: Joaquin Anton <janton@nvidia.com>
@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1663137]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1663137]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1663137]: BUILD PASSED

@jantonguirao jantonguirao merged commit 7813418 into NVIDIA:master Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants