Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Improve apply_prediction #523

Merged
merged 19 commits into from
Mar 5, 2018
Merged

Conversation

Hakuyume
Copy link
Member

@Hakuyume Hakuyume commented Feb 27, 2018

fix #468, fix #516

  • renamed imgs, pred_values, gt_values -> in_values, out_values, rest_values
  • in_values are the first n_input elements of the batch
  • renamed apply_prediction_to_iterator -> apply_to_iterator

@Hakuyume Hakuyume changed the title Improve apply_prediction [WIP] Improve apply_prediction Feb 27, 2018
@Hakuyume Hakuyume changed the title [WIP] Improve apply_prediction Improve apply_prediction Feb 27, 2018

>>> from chainer import iterators
>>>
>>> from chainercv.datasets import VOCDetectionDataset
Copy link
Member

Choose a reason for hiding this comment

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

VOCBBoxDataset. This is my mistake. Sorry.



def apply_to_batch(func, iterator, n_input=1, hook=None):
"""Apply a function/method to an iterator of batches.
Copy link
Member

Choose a reason for hiding this comment

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

This is not directly related to the change proposed in this PR, but this sentence is incorrect.
We apply a function to batches and not an iterator.
Thus, Apply a function/method to batches of data from an iterator is more precise.

Copy link
Member

Choose a reason for hiding this comment

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

or
>>> # batch: [(in_val0, in_val1, ...)]
or
>>> # batch: [(in_val0, in_val1, ..., rest_val0, rest_val1, ...)]
Copy link
Member

Choose a reason for hiding this comment

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

n_input should appear at this part of the documentation.

>>> out_vals = func(in_val0, in_val1, ...)
>>> # out_vals: [out_val]
or
>>> out_vals0, out_vals1, ... = func(in_val0, in_val1)
Copy link
Member

Choose a reason for hiding this comment

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

in_val0, in_val1 --> in_val0, in_val1, ... for consistency

Copy link
Member

Choose a reason for hiding this comment

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

I think these examples are not necessary.

Copy link
Member Author

@Hakuyume Hakuyume Mar 1, 2018

Choose a reason for hiding this comment

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

I would like to show the returned values should be ([out_val0], [out_val1], ...) and not [(out_val0, out_val1, ...)]. I feel it unclear without examples.

Copy link
Member

Choose a reason for hiding this comment

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

Ah. I see. So you wanted to emphasize that the function returns tuple of lists.

BTW, shouldn't function take list of values?
So out_vals0, out_vals1, ... = func(in_val0, in_val1) --> out_vals0, out_vals1, ... = func(in_vals0, in_vals1).

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right. That's my mistake.


This function applies a function/method to an iterator of batches.
It assumes that the iterator returns a batch of input data or
a batch of tuples whose first elements ara input data.
Copy link
Member

Choose a reason for hiding this comment

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

I thought it was better to describe the internals of this function more in the beginning.
I wrote an alternative docstring. What do you think?


This function assumes that the iterator iterates over a collection of tuples that contain inputs to func.
Additionally, the tuples may contain values that are not used by func.
For convenience, we allow the iterator to iterate over a collection of inputs that are not tuple.
t
Conceptually, this function does two operations.
First, it splits the iterator into two iterators. The first iterator contains the inputs to func and the second iterator contains the rest of the data from iterator.
Second, it creates another iterator that iterates over a collection of the outputs of the func.

Here is an illustration of the expected behavior of iterator.
M is the number of arguments for func, which is defined by an argument n_input. N is the number of elements in a tuple from iterator that are not used by func. Note that N can be 0.

>>> batch = next(iterator)
>>> # batch:  [(in_val0, ..., in_val{M-1}, rest_val0, ..., rest_val{N-1})]
or
>>> # batch:  [in_val]

Copy link
Member Author

Choose a reason for hiding this comment

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

First, it splits the iterator into two iterators. The first iterator contains the inputs to func and the second iterator contains the rest of the data from iterator.
Second, it creates another iterator that iterates over a collection of the outputs of the func.

This explanation sounds this function returns three iterators. However, it is not correct.

Copy link
Member

Choose a reason for hiding this comment

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

Right. That is my mistake.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I will try to merge your solution into docstring.


>>> in_values, out_values, rest_values = apply_to_iterator(
>>> func, iterator, n_input)
>>> # in_values: (iter of in_val0, ..., iter of in_val{n_input})
Copy link
Member

Choose a reason for hiding this comment

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

n_input --> n_input-1

def apply_to_iterator(func, iterator, n_input=1, hook=None):
"""Apply a function/method to batches from an iterator.

This function applies a function/method to an iterator of batches.
Copy link
Member

Choose a reason for hiding this comment

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

This function applies a function/method to batches from an iterator of type chainer.Iterator.

This makes clear that we are talking about Chainer's iterator.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is not only for chainer.Iterator. It can work with other iterators. an iterator of type chainer.Iterator sounds it requires some special features.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for pointing out.


It assumes that the iterator iterates over a collection of tuples
that contain inputs to :func:`func`.
Additionally, the tuples may contain values that are not used by func.
Copy link
Member

Choose a reason for hiding this comment

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

s/by func/by the funciton

Copy link
Member

@yuyu2172 yuyu2172 left a comment

Choose a reason for hiding this comment

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

LGTM except one comment.

that are not used by :func:`func`.
For convenience, we allow the iterator to iterate over a collection of
inputs that are not tuple.
Here is an illustration of the expected behavior of the iterator.
Copy link
Member

Choose a reason for hiding this comment

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

I think this comment is helpful for Chainer users.


Here is an ....
In short, we require iterator to satisfy the interface of chainer.Iterator.

Copy link
Member Author

Choose a reason for hiding this comment

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

chainer.Iterator requires many properties, such as batch_size, epoch_detail, etc..
apply_to_iterator does not require these properties.
https://docs.chainer.org/en/stable/reference/generated/chainer.iterators.SerialIterator.html

Copy link
Member Author

Choose a reason for hiding this comment

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

How about chainer.Iteratror satisfies this requirement or chainer.Iteratror satisfies this behaviour? These are correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

This behaviour is same as chainer.Iterator is also OK.

Copy link
Member

Choose a reason for hiding this comment

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

This behaviour is the same as chainer.Iterator

This one looks good! (“the” was missing)

@Hakuyume
Copy link
Member Author

Hakuyume commented Mar 5, 2018

@yuyu2172

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

apply_prediction_to_iterator is too lengthy Consider multiple inputs into model
2 participants