Skip to content

[model/predict] Add support for different types of action outputs #113

Open
ANarayan wants to merge 30 commits intomainfrom
flexibile-action-outputs
Open

[model/predict] Add support for different types of action outputs #113
ANarayan wants to merge 30 commits intomainfrom
flexibile-action-outputs

Conversation

@ANarayan
Copy link
Collaborator

This PR adds support processing action outputs of different types. The current implementation expects that all action outputs are of type tensor. Here, we expand support for outputs of type dict. This PR also adds a new utils function (merge_objects) which merges different types of objects.

@codecov
Copy link

codecov bot commented Oct 29, 2021

Codecov Report

Merging #113 (7f76dbf) into master (03c80bc) will increase coverage by 0.10%.
The diff coverage is 96.66%.

❗ Current head 7f76dbf differs from pull request most recent head 044ed8c. Consider uploading reports for the commit 044ed8c to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #113      +/-   ##
==========================================
+ Coverage   91.21%   91.31%   +0.10%     
==========================================
  Files          40       40              
  Lines        1991     2039      +48     
  Branches      425      446      +21     
==========================================
+ Hits         1816     1862      +46     
  Misses        101      101              
- Partials       74       76       +2     
Flag Coverage Δ
unittests 91.31% <96.66%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/emmental/meta.py 84.46% <ø> (ø)
src/emmental/utils/parse_args.py 99.31% <ø> (ø)
src/emmental/utils/utils.py 96.70% <95.91%> (-0.38%) ⬇️
src/emmental/logging/checkpointer.py 93.33% <100.00%> (ø)
src/emmental/model.py 88.46% <100.00%> (+0.11%) ⬆️

@ANarayan ANarayan requested a review from senwu October 30, 2021 03:30
Copy link
Owner

@senwu senwu left a comment

Choose a reason for hiding this comment

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

Left some comments

output_dict[action_name][output_index].cpu().detach().numpy()
)
action_output = output_dict[action_name][output_index]
if isinstance(action_output, dict):
Copy link
Owner

Choose a reason for hiding this comment

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

Can we move this if-else to move_to_device?

out_dict[task_name][action_name].extend(
out_bdict[task_name][action_name]
)
if out_dict[task_name][action_name] == []:
Copy link
Owner

Choose a reason for hiding this comment

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

Can we move this to merge_objects?

Copy link
Collaborator Author

@ANarayan ANarayan Oct 30, 2021

Choose a reason for hiding this comment

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

problem is that out_dict[task_name][action_name] is always going to be a list, but out_bdict[task_name][action_name] can be any type. Thus, the merge_objects function will just raise a type error because of type mismatch

Copy link
Owner

Choose a reason for hiding this comment

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

this should be gone right?


Args:
obj_1: first object.
obj_2: seecond object to be merged into the first object.
Copy link
Owner

Choose a reason for hiding this comment

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

typo?

Args:
obj_1: first object.
obj_2: seecond object to be merged into the first object.

Copy link
Owner

Choose a reason for hiding this comment

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

Can you add more description about this function here?

Returns:
an object reflecting the merged output of the two inputs.
"""
if isinstance(obj_1, torch.Tensor):
Copy link
Owner

Choose a reason for hiding this comment

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

We need check the two objects are the same type right?

return obj_1
else:
for key, value in obj_1.items():
if isinstance(value, torch.Tensor):
Copy link
Owner

Choose a reason for hiding this comment

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

also include np.array?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@senwu isn't the type of a np.array <class 'numpy.ndarray'>

elif isinstance(obj_1, list):
obj_1.extend(obj_2)
return obj_1
elif isinstance(obj_1, tuple):
Copy link
Owner

Choose a reason for hiding this comment

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

Tuple might have more than 2 objects.


assert torch.equal(
merge_objects(torch.Tensor([1, 2]), torch.Tensor([2, 3])),
torch.Tensor([1, 2, 2, 3]),
Copy link
Owner

Choose a reason for hiding this comment

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

This should be torch.Tensor[[1, 2], [2, 3]]? We want to merge based on the first dim.

torch.Tensor([1, 2, 2, 3]),
)
assert np.array_equal(
merge_objects(np.array([1, 2]), np.array([2, 3])), np.array([1, 2, 2, 3])
Copy link
Owner

Choose a reason for hiding this comment

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

Save here?

[3, 4, 4, 5],
)
assert merge_objects([1, 2, 3], [2, 3, 4]) == [1, 2, 3, 2, 3, 4]

Copy link
Owner

Choose a reason for hiding this comment

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

can we have more use cases about np.array, torch.Tensor?

Copy link
Owner

@senwu senwu left a comment

Choose a reason for hiding this comment

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

Left few comments

out_dict[task_name][action_name].extend(
out_bdict[task_name][action_name]
)
if out_dict[task_name][action_name] == []:
Copy link
Owner

Choose a reason for hiding this comment

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

this should be gone right?


assert len(test4_pred["outputs"]["task3"]["input1_t3_out"]["image_pil"]) == 10
assert isinstance(
test4_pred["outputs"]["task3"]["input1_t3_out"]["image_pil"], np.ndarray
Copy link
Owner

Choose a reason for hiding this comment

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

Can we check the shape?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

for #113 (comment), we can't get rid of it because out_dict[task_name][action_name] and out_bdict[task_name][action_name] may not be of the same type for the empty check to be handled by merge_objects

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.

2 participants