Skip to content

decode_gif: add transparency support and image offsets - #8419

Merged
NicolasHug merged 4 commits into
pytorch:mainfrom
NicolasHug:gif_transparency
May 20, 2024
Merged

decode_gif: add transparency support and image offsets#8419
NicolasHug merged 4 commits into
pytorch:mainfrom
NicolasHug:gif_transparency

Conversation

@NicolasHug

@NicolasHug NicolasHug commented May 9, 2024

Copy link
Copy Markdown
Member

This PR now properly handles transparency within the GIF, and also allows each individual image to start at its own Top and Left offset. For docs on how transparency is encoded in GIFs, see https://giflib.sourceforge.net/whatsinagif/animation_and_transparency.html

Turns out the decoder was unable to properly decode https://en.wikipedia.org/wiki/GIF#/media/File:Rotating_earth_(large).gif, but now it's fine.

Benchmark. Slighly slower than before but the relative speed compared with PIL is similar to before.

Details
fire.gif                   size=[33, 3, 60, 30]
tv : med = 0.87ms +- 0.07
pil: med = 1.30ms +- 0.15
tv is 1.50x faster

gifgrid.gif                size=[3, 100, 100]
tv : med = 0.10ms +- 0.02
pil: med = 0.10ms +- 0.02
tv is 0.94x faster

porsche.gif                size=[3, 200, 320]
tv : med = 0.68ms +- 0.08
pil: med = 0.29ms +- 0.03
tv is 0.43x faster

solid2.gif                 size=[3, 400, 640]
tv : med = 2.46ms +- 0.15
pil: med = 0.90ms +- 0.12
tv is 0.37x faster

treescap-interlaced.gif    size=[3, 40, 40]
tv : med = 0.03ms +- 0.01
pil: med = 0.07ms +- 0.01
tv is 1.95x faster

treescap.gif               size=[3, 40, 40]
tv : med = 0.03ms +- 0.00
pil: med = 0.07ms +- 0.01
tv is 2.19x faster

welcome2.gif               size=[6, 3, 48, 290]
tv : med = 0.90ms +- 0.06
pil: med = 1.06ms +- 0.09
tv is 1.19x faster

x-trans.gif                size=[3, 100, 100]
tv : med = 0.09ms +- 0.01
pil: med = 0.11ms +- 0.02
tv is 1.23x faster

grace.gif                  size=[3, 606, 517]
tv : med = 4.31ms +- 0.23
pil: med = 2.70ms +- 0.12
tv is 0.63x faster

import torch
from time import perf_counter_ns


def bench(f, *args, num_exp=100, warmup=0, **kwargs):

    for _ in range(warmup):
        f(*args, **kwargs)

    times = []
    for _ in range(num_exp):
        start = perf_counter_ns()
        f(*args, **kwargs)
        end = perf_counter_ns()
        times.append(end - start)
    return torch.tensor(times).float()

def report_stats(times, unit="ms"):
    mul = {
        "ns": 1,
        "µs": 1e-3,
        "ms": 1e-6,
        "s": 1e-9,
    }[unit]
    times = times * mul
    std = times.std().item()
    med = times.median().item()
    print(f"{med = :.2f}{unit} +- {std:.2f}")
    return med


from PIL import Image, ImageSequence
from torchvision import io
from pathlib import Path

# files from https://sourceforge.net/p/giflib/code/ci/master/tree/pic/
paths = list(Path("/home/nicolashug/dev/giflib-code/pic/").glob("*.gif"))
paths += [Path("/home/nicolashug/grace.gif")]  # grace_hopper from torchvision/test/assets

def read_image_pil(path):
    for img in ImageSequence.Iterator(Image.open(path)):
        img.convert("RGB")

for path in paths:
    print()
    print(f"{path.name:<26} size={list(io.read_image(path).shape)}")
    print("tv : ", end="")
    times = bench(io.read_image, path, warmup=10)
    tv_med = report_stats(times)
    print("pil: ", end="")
    times = bench(read_image_pil, path, warmup=10)
    pil_med = report_stats(times)
    print(f"tv is {pil_med / tv_med:.2f}x faster")

@pytorch-bot

pytorch-bot Bot commented May 9, 2024

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/8419

Note: Links to docs will display an error until the docs builds have been completed.

❌ 8 New Failures

As of commit 8c228f5 with merge base 51429c2 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Comment thread torchvision/csrc/io/image/cpu/decode_gif.cpp Outdated

@vfdev-5 vfdev-5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks @NicolasHug !

@NicolasHug
NicolasHug merged commit 61d97f4 into pytorch:main May 20, 2024
@github-actions

Copy link
Copy Markdown

Hey @NicolasHug!

You merged this PR, but no labels were added.
The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

facebook-github-bot pushed a commit that referenced this pull request May 20, 2024
Reviewed By: vmoens

Differential Revision: D57565238

fbshipit-source-id: 06cbf21115c79950711f6100832a06a7f495ca81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants