BachiLi / diffvg

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

RuntimeError in the sketch GAN example #18

Open oeway opened 3 years ago

oeway commented 3 years ago

Hi, I am trying the sketch GAN example, but failed to run it because of RuntimeError: The total length of the shape boundaries in the scene is not a number. Length = -nan.

If I run the following code for example:

g = Generator()
g(torch.randn([1, 100]))

I got:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-7f54cfbd38f5> in <module>
      1 g = Generator()
      2 t = torch.randn([1, 100])
----> 3 g(t)

~/miniconda3/envs/diffvg/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

<ipython-input-1-3543db5b47a9> in forward(self, z)
     93                          random.randint(0, 1048576), # seed
     94                          None,
---> 95                          *scene_args)
     96             img = img[:, :, :1]
     97             # HWC -> NCHW

~/miniconda3/envs/diffvg/lib/python3.7/site-packages/pydiffvg/render_pytorch.py in forward(ctx, width, height, num_samples_x, num_samples_y, seed, background_image, *args)
    366         scene = diffvg.Scene(canvas_width, canvas_height,
    367             shapes, shape_groups, filt, pydiffvg.get_use_gpu(),
--> 368             pydiffvg.get_device().index if pydiffvg.get_device().index is not None else -1)
    369         time_elapsed = time.time() - start
    370         global print_timing

RuntimeError: The total length of the shape boundaries in the scene is not a number. Length = -nan

Any idea why?

oeway commented 3 years ago

A quick note here, after changing the following lines, the code runs:

                points = img_size * out[b, index: index + 2 * (num_segments + 1)].view(-1, 2).cpu()
                index += 2 * (num_segments + 1)
                stroke_width = out[b, index].cpu()
                index += 1

                # the following line need to be changed
                num_control_points = torch.zeros(int(np.log2(num_segments)), dtype = torch.int32).cpu() + 2
                path = pydiffvg.Path(num_control_points = num_control_points,
                                     points = points,
                                     stroke_width = stroke_width,
                                     is_closed = False)

However, I don't know how exactly should we compute the num_control_points from the num_segments. @BachiLi Could you please give some hint here?

I had to also pin the version of torch-tools to 0.0.35, otherwise the training cannot be started.