|
import pydiffvg |
|
import torch |
|
import skimage |
|
import numpy as np |
|
|
|
|
|
pydiffvg.set_use_gpu(torch.cuda.is_available()) |
|
|
|
canvas_width, canvas_height = 256, 256 |
|
|
|
points = torch.tensor([[120.0, 30.0], |
|
[ 60.0, 218.0], |
|
[210.0, 98.0], |
|
[ 30.0, 98.0], |
|
[180.0, 218.0]]) |
|
polygon = pydiffvg.Polygon(points = points, is_closed = True) |
|
shapes = [polygon] |
|
polygon_group = pydiffvg.ShapeGroup(shape_ids = torch.tensor([0]), |
|
fill_color = torch.tensor([0.3, 0.6, 0.3, 1.0])) |
|
shape_groups = [polygon_group] |
|
scene_args = pydiffvg.RenderFunction.serialize_scene(\ |
|
canvas_width, canvas_height, shapes, shape_groups) |
|
|
|
render = pydiffvg.RenderFunction.apply |
|
img = render(256, |
|
256, |
|
2, |
|
2, |
|
0, |
|
None, |
|
*scene_args) |
|
|
|
pydiffvg.imwrite(img.cpu(), 'results/single_polygon/target.png', gamma=2.2) |
|
target = img.clone() |
|
|
|
|
|
|
|
points_n = torch.tensor([[140.0 / 256.0, 20.0 / 256.0], |
|
[ 65.0 / 256.0, 228.0 / 256.0], |
|
[215.0 / 256.0, 100.0 / 256.0], |
|
[ 35.0 / 256.0, 90.0 / 256.0], |
|
[160.0 / 256.0, 208.0 / 256.0]], requires_grad=True) |
|
color = torch.tensor([0.3, 0.2, 0.5, 1.0], requires_grad=True) |
|
polygon.points = points_n * 256 |
|
polygon_group.color = color |
|
scene_args = pydiffvg.RenderFunction.serialize_scene(\ |
|
canvas_width, canvas_height, shapes, shape_groups) |
|
img = render(256, |
|
256, |
|
2, |
|
2, |
|
1, |
|
None, |
|
*scene_args) |
|
pydiffvg.imwrite(img.cpu(), 'results/single_polygon/init.png', gamma=2.2) |
|
|
|
|
|
optimizer = torch.optim.Adam([points_n, color], lr=1e-2) |
|
|
|
for t in range(100): |
|
print('iteration:', t) |
|
optimizer.zero_grad() |
|
|
|
polygon.points = points_n * 256 |
|
polygon_group.color = color |
|
scene_args = pydiffvg.RenderFunction.serialize_scene(\ |
|
canvas_width, canvas_height, shapes, shape_groups) |
|
img = render(256, |
|
256, |
|
2, |
|
2, |
|
t+1, |
|
None, |
|
*scene_args) |
|
|
|
pydiffvg.imwrite(img.cpu(), 'results/single_polygon/iter_{}.png'.format(t), gamma=2.2) |
|
|
|
loss = (img - target).pow(2).sum() |
|
print('loss:', loss.item()) |
|
|
|
|
|
loss.backward() |
|
|
|
print('points_n.grad:', points_n.grad) |
|
print('color.grad:', color.grad) |
|
|
|
|
|
optimizer.step() |
|
|
|
print('points:', polygon.points) |
|
print('color:', polygon_group.fill_color) |
|
|
|
|
|
polygon.points = points_n * 256 |
|
polygon_group.color = color |
|
scene_args = pydiffvg.RenderFunction.serialize_scene(\ |
|
canvas_width, canvas_height, shapes, shape_groups) |
|
img = render(256, |
|
256, |
|
2, |
|
2, |
|
102, |
|
None, |
|
*scene_args) |
|
|
|
pydiffvg.imwrite(img.cpu(), 'results/single_polygon/final.png') |
|
|
|
|
|
from subprocess import call |
|
call(["ffmpeg", "-framerate", "24", "-i", |
|
"results/single_polygon/iter_%d.png", "-vb", "20M", |
|
"results/single_polygon/out.mp4"]) |
|
|