Skip to content

modified code of io.read_video and io.read_video_timestamps to intepret pts values in seconds #1331

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

Merged
merged 4 commits into from
Sep 30, 2019

Conversation

cskanani
Copy link
Contributor

The start_pts and end_pts attributes of io.read_video method were not having any time unit. It was not clear what start_pts and end_pts values signifies (this values depends on stream.time_base time unit).
Modified the code of io.read_video and io.read_video_timestamps to intepret pts values in seconds, stream.time_base is used for conversion of seconds and pts values.

@cskanani
Copy link
Contributor Author

@fmassa @bjuncek @stephenyan1231 please check this PR.

video_end_pts = end_pts
video_stream = container.streams.video[0]
if pts_unit == 'sec':
video_start_pts = math.floor(start_pts * (1 / video_stream.time_base))
Copy link
Contributor

Choose a reason for hiding this comment

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

will math.floor cast it to int?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, for python2 it is returning answer as float and for python3 it is returning int.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I have converted ouput of math.floor to int for handling the python2 problem.

Copy link
Contributor

@bjuncek bjuncek left a comment

Choose a reason for hiding this comment

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

Thanks very much for the PR!!!
Looks good to me - haven't tested it out yet though.

container.close()
if pts_unit == 'sec':
return [float(x.pts * video_time_base) for x in video_frames], video_fps
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a reason we're casting it to floats rather than keeping it as fractions (just asking).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For making it more understandable and uniform, i.e. for read_video method users will pass values of start_pts and end_pts in floats. Therefore, I think it is good to return the values in same format it would make it more understandable.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with you that returning floats might be more natural, but I'm worries that we might be introducing some unnecessary precision loss due to this cast.
I think we should return the values as Fraction, and handle both Fraction and float in read_video (which should be already the case or almost).

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed code to return Fraction from read_video_timestamps. The read_video was already handling both Fractions and floats, added that info to the doc string to make it more clear.

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the PR!

Can you also add a test comparing that there is no difference in results while reading using seconds or pts? We now have a few test videos in the repo, so you can use those as well, instead of just using the randomly generated videos.

container.close()
if pts_unit == 'sec':
return [float(x.pts * video_time_base) for x in video_frames], video_fps
Copy link
Member

Choose a reason for hiding this comment

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

I agree with you that returning floats might be more natural, but I'm worries that we might be introducing some unnecessary precision loss due to this cast.
I think we should return the values as Fraction, and handle both Fraction and float in read_video (which should be already the case or almost).

Thoughts?

@cskanani
Copy link
Contributor Author

Thanks a lot for the PR!

Can you also add a test comparing that there is no difference in results while reading using seconds or pts? We now have a few test videos in the repo, so you can use those as well, instead of just using the randomly generated videos.

I have added three test cases using pts_unit as 'sec'.

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks a lot!

Test failures do not seem to be related to your change.

I have one last comment, let me know if you think it could be improved.

video_start_pts = start_pts
video_end_pts = end_pts
video_stream = container.streams.video[0]
if pts_unit == 'sec':
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we can't move this conversion logic to _read_from_stream? It could avoid duplicating the implementation for both audio and video.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I have moved the unit conversion logic to _read_from_stream method.

@codecov-io
Copy link

Codecov Report

Merging #1331 into master will decrease coverage by 0.48%.
The diff coverage is 45.16%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #1331      +/-   ##
=========================================
- Coverage   65.89%   65.4%   -0.49%     
=========================================
  Files          75      75              
  Lines        5785    5851      +66     
  Branches      885     900      +15     
=========================================
+ Hits         3812    3827      +15     
- Misses       1708    1752      +44     
- Partials      265     272       +7
Impacted Files Coverage Δ
torchvision/io/video.py 66.01% <45.16%> (-5.31%) ⬇️
torchvision/models/detection/transform.py 66.92% <0%> (-11.38%) ⬇️
torchvision/ops/roi_align.py 69.23% <0%> (-2.2%) ⬇️
torchvision/ops/roi_pool.py 72% <0%> (-2.08%) ⬇️
torchvision/datasets/voc.py 21.64% <0%> (-1.43%) ⬇️
torchvision/transforms/transforms.py 80.98% <0%> (-0.59%) ⬇️
torchvision/ops/boxes.py 94.44% <0%> (-0.3%) ⬇️
torchvision/models/detection/roi_heads.py 55.77% <0%> (-0.16%) ⬇️
torchvision/ops/__init__.py 100% <0%> (ø) ⬆️
torchvision/ops/_custom_ops.py
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a129b6b...899a6e9. Read the comment docs.

@cskanani
Copy link
Contributor Author

Looks good to me, thanks a lot!

Test failures do not seem to be related to your change.

I have one last comment, let me know if you think it could be improved.

Hi, I have made the changes and moved the unit conversion logic to _read_from_stream method for reducing the duplication. The build is failing on macos but it is not caused by my changes. Let me know if some changes are required in the code.

@bjuncek
Copy link
Contributor

bjuncek commented Sep 27, 2019

Looks good to me!

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

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

LGTM, thanks a lot for the awesome PR @cskanani !

@fmassa fmassa merged commit 17e355f into pytorch:master Sep 30, 2019
@fmassa fmassa mentioned this pull request Oct 31, 2019
fmassa pushed a commit that referenced this pull request Oct 31, 2019
…et pts values in seconds (#1331)

* modified code of io.read_video and io.read_video_timestamps to interpret pts values in seconds

* changed default value for pts_unit to pts, corrected formatting

* hanndliing both fractions and floats for start_pts and end_pts, added test cases for pts_unit sec

* moved unit conversion logic to _read_from_stream method
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