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 PeekShape operator to learn the decoded image shape #2205

Merged
merged 2 commits into from
Aug 12, 2020

Conversation

JanuszL
Copy link
Contributor

@JanuszL JanuszL commented Aug 11, 2020

  • adds an operator that utilizes peek shape functionality from Image class that can parse image header without decoding to learn decoded image shape

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

Why we need this PR?

Pick one, remove the rest

  • It adds PeekShape operator to learn the decoded image shape

What happened in this PR?

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

  • What solution was applied:
    PeekShape operator to learn the decoded image shape
  • Affected modules and functionalities:
    new operator
  • Key points relevant for the review:
    NA
  • Validation and testing:
    test case is added
  • Documentation (including examples):
    NA

JIRA TASK: [DALI-1564]

@JanuszL
Copy link
Contributor Author

JanuszL commented Aug 11, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1536126]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1536126]: BUILD FAILED

@JanuszL
Copy link
Contributor Author

JanuszL commented Aug 11, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1536191]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1536191]: BUILD FAILED

- adds an operator that utilizes peek shape functionality from
  Image class that can parse image header without decoding to
  learn decoded image shape

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

JanuszL commented Aug 11, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1536371]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1536371]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1536371]: BUILD PASSED


namespace dali {

DALI_SCHEMA(PeekShape)
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 image specific, so I think the name should reflect it. Something like ImagePeekShape?

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

}

template <typename type>
void ConvertShape(Tensor<CPUBackend> &out, const TensorShape<3> &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
void ConvertShape(Tensor<CPUBackend> &out, const TensorShape<3> &shape) {
void WriteShape(Tensor<CPUBackend> &out, const TensorShape<3> &shape) {

?

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

pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
input, _ = fn.file_reader(file_root=data_path, shard_id=0, num_shards=1, name="reader")
decoded = fn.image_decoder(input, device=device, output_type=types.RGB)
if device == "cpu" :
Copy link
Contributor

Choose a reason for hiding this comment

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

shape_device = 'cpu' if device == 'cpu' else 'gpu'

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

out = pipe.run()
(image, decoded_shape, raw_shape) = [v.as_cpu() if isinstance(v, dali.backend_impl.TensorListGPU) else v for v in out]
for i in range(batch_size):
# as we are asking for a particular color space it may differ from the source image, so don't comapre it
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
# as we are asking for a particular color space it may differ from the source image, so don't comapre it
# as we are asking for a particular color space it may differ from the source image, so don't compare it

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


test_types = [types.INT32, types.UINT32, types.INT64, types.UINT64, types.FLOAT, types.FLOAT64]

def test_image_decoder():
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
def test_image_decoder():
def test_operator_peek_shape():

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

test_types = [types.INT32, types.UINT32, types.INT64, types.UINT64, types.FLOAT, types.FLOAT64]

def test_image_decoder():
for device in {'cpu', 'mixed'}:
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 device in {'cpu', 'mixed'}:
for device in {'cpu', 'gpu'}:

I'd rather set the device of the PeekShape operator here and calculate the device for decoder inside the function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Peek shape is available only for the CPU, so it doesn't make much sense. Maybe I should just test for the CPU, after all.

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

# as we are asking for a particular color space it may differ from the source image, so don't comapre it
for d in range(len(image.at(i).shape) - 1):
shape_type = dali_to_numpy[out_type]
assert image.at(i).shape[d] == decoded_shape.at(i)[d], "{} vs {}".format(image.at(i).shape[d], decoded_shape.at(i)[d])
Copy link
Contributor

Choose a reason for hiding this comment

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

extract image.at(i) outside of the loop?

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

for i in range(batch_size):
# as we are asking for a particular color space it may differ from the source image, so don't comapre it
for d in range(len(image.at(i).shape) - 1):
shape_type = dali_to_numpy[out_type]
Copy link
Contributor

Choose a reason for hiding this comment

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

This can go out of the loop

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

path = 'db/single'
file_types = {'jpeg', 'mixed', 'png', 'tiff', 'pnm', 'bmp'}

dali_to_numpy = {
Copy link
Contributor

Choose a reason for hiding this comment

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

this could go to utils

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 Aug 12, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1538872]: BUILD STARTED

namespace dali {

DALI_SCHEMA(PeekImageShape)
.DocStr(R"code(Obtain the shape of the encoded image.)code")
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"code(Obtain the shape of the encoded image.)code")
.DocStr(R"code(Obtains the shape of the encoded image.)code")

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

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

JanuszL commented Aug 12, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1538921]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1538872]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1538872]: BUILD PASSED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1538921]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1538921]: BUILD PASSED

@JanuszL JanuszL merged commit 0e79ccc into NVIDIA:master Aug 12, 2020
@JanuszL JanuszL deleted the peek_shape branch August 12, 2020 21:24
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.

4 participants