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

Fix precision loss in CropWindowGenerator #1735

Merged
merged 1 commit into from
Feb 18, 2020
Merged

Fix precision loss in CropWindowGenerator #1735

merged 1 commit into from
Feb 18, 2020

Conversation

JanuszL
Copy link
Contributor

@JanuszL JanuszL commented Feb 14, 2020

  • CropWindowGenerator computes anchor and shapes as a rel_anch * shape, rel_shape * shape. To be more accurate it should be rel_anch * shape, (rel_anch + rel_shape) * shape - (rel_anch * shape)
  • adds a special case when anchors and shapes are relative. When only one or none is then the old code path is used
  • adds rounding in place of truncation
  • updates tests

Signed-off-by: Janusz Lisiecki jlisiecki@nvidia.com

Why we need this PR?

Pick one, remove the rest

  • It fixes a bug in CropWindowGenerator that was caused by float point operation error accumulation

What happened in this PR?

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

  • What solution was applied:
    adds a special case when anchors and shapes are relative. When only one or none is then the old code path is used
    adds rounding in place of truncation
  • Affected modules and functionalities:
    slice_attr
  • Key points relevant for the review:
    NA
  • Validation and testing:
    Updated tests are run on CI
  • Documentation (including examples):
    NA

JIRA TASK: [NA]

@JanuszL
Copy link
Contributor Author

JanuszL commented Feb 14, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1129258]: BUILD STARTED

int64_t slice_end;
// special case - minimize the floating point error by multiplying only once after sum
if (normalized_anchor_ && normalized_shape_) {
slice_end = static_cast<int64_t>((anchor_val + shape_val) * shape[dim]);
Copy link
Contributor

@mzient mzient Feb 14, 2020

Choose a reason for hiding this comment

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

In my opinion, we should be rounding, not truncating. This would make relative slice flip-invariant (now it's not).

Copy link
Contributor

Choose a reason for hiding this comment

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

...and the loss of precision, if any, is rather marginal. If we care for extreme cases (slicing huge 1D objects, for example), this should be a double anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is marginal but still there is a divergence between implementations. So I wanted to make that consistent. Will switch to round.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

if (normalized_shape_) {
shape_val *= shape[dim];
}
slice_end = static_cast<int64_t>(anchor_val + shape_val);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1129364]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1129454]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1129454]: BUILD STARTED

1 similar comment
@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1129454]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1129258]: BUILD PASSED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1129454]: BUILD PASSED

@@ -238,24 +238,25 @@ def slice_func_helper(axes, axis_names, layout, normalized_anchor, normalized_sh
full_slice_anchor[axis] = slice_anchor[idx]
full_slice_shape[axis] = slice_shape[idx]

#std::round has different behaviour than np.round so manually ad 0.5 and truncate to int
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
#std::round has different behaviour than np.round so manually ad 0.5 and truncate to int
#std::round has different behaviour than np.round so manually add 0.5 and truncate to int

;)

if (normalized_shape_) {
shape_val *= shape[dim];
}
slice_end = std::llround(anchor_val + shape_val);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not correct, especially for really big data. Cast to double or calculate slice_end using integer start/end in respective if branches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@JanuszL
Copy link
Contributor Author

JanuszL commented Feb 18, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1134768]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1134768]: BUILD FAILED

- CropWindowGenerator computes anchor and shapes as
  a rel_anch * shape, rel_shape * shape. To be more accurate it should
  be rel_anch * shape, (rel_anch + rel_shape) * shape - (rel_anch * shape)
- adds a special case when anchors and shapes are relative. When only one or
  none is then the old code path is used
- adds rounding in place of truncation
- updates tests
- uses doubles in place of floats to improve precision

Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
@JanuszL
Copy link
Contributor Author

JanuszL commented Feb 18, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1135044]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1135044]: BUILD PASSED

@JanuszL JanuszL merged commit 558889d into NVIDIA:master Feb 18, 2020
@JanuszL JanuszL deleted the fix_random_crop branch February 18, 2020 22:37
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