BachiLi / diffvg

Differentiable Vector Graphics Rasterization
https://people.csail.mit.edu/tzumao/diffvg/
Apache License 2.0
962 stars 159 forks source link

RuntimeError: The total length of the shape boundaries in the scene is equal or less than 0. Length = 0.000000 #42

Closed IzhanVarsky closed 2 years ago

IzhanVarsky commented 2 years ago

Hi. I tried to create a circle using diffvg. Here's my code:

import pydiffvg
import torch

canvas_width, canvas_height = 512, 512
points = torch.tensor([256, 226,
                       226, 226, 226, 256, 226, 256,
                       226, 256, 226, 286, 256, 286,
                       286, 286, 286, 256, 286, 256,
                       286, 226, 256, 226, 256, 226]).view(-1, 2)
path = pydiffvg.Path(num_control_points=torch.tensor([2, 2, 2, 2]),
                     points=points,
                     stroke_width=torch.tensor(0.5),
                     is_closed=True)
shapes = [path]
path_group = pydiffvg.ShapeGroup(shape_ids=torch.tensor([0]), fill_color=torch.tensor([0.3, 0.6, 0.3, 1.0]))
shape_groups = [path_group]
scene_args = pydiffvg.RenderFunction.serialize_scene(canvas_width, canvas_height, shapes, shape_groups)
render = pydiffvg.RenderFunction.apply
img = render(canvas_width, canvas_height, 2, 2, 0, None, *scene_args)
pydiffvg.imwrite(img.cpu(), "target.png", gamma=2.2)
target = img.clone()

But this fails with error:

Traceback (most recent call last):
  File "xxx.py", line 26, in <module>
    *scene_args)
  File "...venv\lib\site-packages\diffvg-0.0.1-py3.7-win-amd64.egg\pydiffvg\render_pytorch.py", line 368, in forward
    pydiffvg.get_device().index if pydiffvg.get_device().index is not None else -1)
RuntimeError: The total length of the shape boundaries in the scene is equal or less than 0. Length = 0.000000

I don't think this is normal.

IzhanVarsky commented 2 years ago

The solution of the problem is to change the type of points tensor from int64 to float. For example using .float() method:

points = torch.tensor([256, 226,
                       226, 226, 226, 256, 226, 256,
                       226, 256, 226, 286, 256, 286,
                       286, 286, 286, 256, 286, 256,
                       286, 226, 256, 226, 256, 226]).view(-1, 2).float()
nityanandmathur commented 1 year ago

How to change type of points from int to float when using the function from_svg_path(), as we specify only the svg path and not the points in function?