diff --git a/apps/single_curve_tf.py b/apps/single_curve_tf.py new file mode 100644 index 0000000..cbd7634 --- /dev/null +++ b/apps/single_curve_tf.py @@ -0,0 +1,109 @@ +import pydiffvg_tensorflow as pydiffvg +import tensorflow as tf +import skimage +import numpy as np + +canvas_width, canvas_height = 256, 256 +num_control_points = tf.constant([2, 2, 2]) + +points = tf.constant([[120.0, 30.0], # base + [150.0, 60.0], # control point + [ 90.0, 198.0], # control point + [ 60.0, 218.0], # base + [ 90.0, 180.0], # control point + [200.0, 65.0], # control point + [210.0, 98.0], # base + [220.0, 70.0], # control point + [130.0, 55.0]]) # control point +path = pydiffvg.Path(num_control_points = num_control_points, + points = points, + is_closed = True) +shapes = [path] +path_group = pydiffvg.ShapeGroup( shape_ids = tf.constant([0], dtype=tf.int32), + fill_color = tf.constant([0.3, 0.6, 0.3, 1.0])) +shape_groups = [path_group] +scene_args = pydiffvg.serialize_scene(\ + canvas_width, canvas_height, shapes, shape_groups) +render = pydiffvg.render +img = render(tf.constant(256), # width + tf.constant(256), # height + tf.constant(2), # num_samples_x + tf.constant(2), # num_samples_y + tf.constant(0), # seed + *scene_args) +# The output image is in linear RGB space. Do Gamma correction before saving the image. +pydiffvg.imwrite(img, 'results/single_curve_tf/target.png', gamma=2.2) +target = tf.identity(img) + +# Move the path to produce initial guess +# normalize points for easier learning rate +points_n = tf.Variable([[100.0/256.0, 40.0/256.0], # base + [155.0/256.0, 65.0/256.0], # control point + [100.0/256.0, 180.0/256.0], # control point + [ 65.0/256.0, 238.0/256.0], # base + [100.0/256.0, 200.0/256.0], # control point + [170.0/256.0, 55.0/256.0], # control point + [220.0/256.0, 100.0/256.0], # base + [210.0/256.0, 80.0/256.0], # control point + [140.0/256.0, 60.0/256.0]]) # control point + +color = tf.Variable([0.3, 0.2, 0.5, 1.0]) +path.points = points_n * 256 +path_group.fill_color = color +scene_args = pydiffvg.serialize_scene(\ + canvas_width, canvas_height, shapes, shape_groups) +img = render(tf.constant(256), # width + tf.constant(256), # height + tf.constant(2), # num_samples_x + tf.constant(2), # num_samples_y + tf.constant(1), # seed + *scene_args) +pydiffvg.imwrite(img, 'results/single_curve_tf/init.png', gamma=2.2) + +optimizer = tf.compat.v1.train.AdamOptimizer(1e-2) + +for t in range(100): + print('iteration:', t) + + with tf.GradientTape() as tape: + # Forward pass: render the image. + path.points = points_n * 256 + path_group.fill_color = color + # Important to use a different seed every iteration, otherwise the result + # would be biased. + scene_args = pydiffvg.serialize_scene(\ + canvas_width, canvas_height, shapes, shape_groups) + img = render(tf.constant(256), # width + tf.constant(256), # height + tf.constant(2), # num_samples_x + tf.constant(2), # num_samples_y + tf.constant(t+1), # seed, + *scene_args) + loss_value = tf.reduce_sum(tf.square(img - target)) + + print(f"loss_value: {loss_value}") + pydiffvg.imwrite(img, 'results/single_curve_tf/iter_{}.png'.format(t)) + + grads = tape.gradient(loss_value, [points_n, color]) + print(grads) + optimizer.apply_gradients(zip(grads, [points_n, color])) + +# Render the final result. +path.points = points_n * 256 +path_group.fill_color = color +scene_args = pydiffvg.serialize_scene(\ + canvas_width, canvas_height, shapes, shape_groups) +img = render(tf.constant(256), # width + tf.constant(256), # height + tf.constant(2), # num_samples_x + tf.constant(2), # num_samples_y + tf.constant(101), # seed + *scene_args) +# Save the images and differences. +pydiffvg.imwrite(img, 'results/single_curve_tf/final.png') + +# Convert the intermediate renderings to a video. +from subprocess import call +call(["ffmpeg", "-framerate", "24", "-i", + "results/single_curve_tf/iter_%d.png", "-vb", "20M", + "results/single_curve_tf/out.mp4"]) diff --git a/apps/single_stroke_tf.py b/apps/single_stroke_tf.py new file mode 100644 index 0000000..42ad5fb --- /dev/null +++ b/apps/single_stroke_tf.py @@ -0,0 +1,109 @@ +import pydiffvg_tensorflow as pydiffvg +import tensorflow as tf +import skimage +import numpy as np + +canvas_width, canvas_height = 256, 256 +num_control_points = tf.constant([2]) + +points = tf.constant([[120.0, 30.0], # base + [150.0, 60.0], # control point + [ 90.0, 198.0], # control point + [ 60.0, 218.0]]) # base +path = pydiffvg.Path(num_control_points = num_control_points, + points = points, + is_closed = False, + stroke_width = tf.constant(15.0)) + +shapes = [path] +path_group = pydiffvg.ShapeGroup( shape_ids = tf.constant([0], dtype=tf.int32), + fill_color = tf.constant([0.0, 0.0, 0.0, 0.0]), + stroke_color = tf.constant([0.6, 0.3, 0.6, 0.8])) +shape_groups = [path_group] +scene_args = pydiffvg.serialize_scene(\ + canvas_width, canvas_height, shapes, shape_groups) +render = pydiffvg.render +img = render(tf.constant(256), # width + tf.constant(256), # height + tf.constant(2), # num_samples_x + tf.constant(2), # num_samples_y + tf.constant(0), # seed + *scene_args) +# The output image is in linear RGB space. Do Gamma correction before saving the image. +pydiffvg.imwrite(img, 'results/single_stroke_tf/target.png', gamma=2.2) +target = tf.identity(img) + + +# Move the path to produce initial guess +# normalize points for easier learning rate +points_n = tf.Variable([[100.0/256.0, 40.0/256.0], # base + [155.0/256.0, 65.0/256.0], # control point + [100.0/256.0, 180.0/256.0], # control point + [ 65.0/256.0, 238.0/256.0]] # base + ) +stroke_color = tf.Variable([0.4, 0.7, 0.5, 0.5]) +stroke_width_n = tf.Variable(5.0 / 100.0) +path.points = points_n * 256 +path.stroke_width = stroke_width_n * 100 +path_group.stroke_color = stroke_color +scene_args = pydiffvg.serialize_scene(\ + canvas_width, canvas_height, shapes, shape_groups) +img = render(tf.constant(256), # width + tf.constant(256), # height + tf.constant(2), # num_samples_x + tf.constant(2), # num_samples_y + tf.constant(1), # seed + *scene_args) +pydiffvg.imwrite(img, 'results/single_stroke_tf/init.png', gamma=2.2) + + + +optimizer = tf.compat.v1.train.AdamOptimizer(1e-2) + +for t in range(100): + print('iteration:', t) + + with tf.GradientTape() as tape: + # Forward pass: render the image. + path.points = points_n * 256 + path.stroke_width = stroke_width_n * 100 + path_group.stroke_color = stroke_color + # Important to use a different seed every iteration, otherwise the result + # would be biased. + scene_args = pydiffvg.serialize_scene(\ + canvas_width, canvas_height, shapes, shape_groups) + img = render(tf.constant(256), # width + tf.constant(256), # height + tf.constant(2), # num_samples_x + tf.constant(2), # num_samples_y + tf.constant(t+1), # seed, + *scene_args) + loss_value = tf.reduce_sum(tf.square(img - target)) + + print(f"loss_value: {loss_value}") + pydiffvg.imwrite(img, 'results/single_stroke_tf/iter_{}.png'.format(t)) + + grads = tape.gradient(loss_value, [points_n, stroke_width_n, stroke_color]) + print(grads) + optimizer.apply_gradients(zip(grads, [points_n, stroke_width_n, stroke_color])) + + +# Render the final result. +path.points = points_n * 256 +path_group.stroke_color = stroke_color +scene_args = pydiffvg.serialize_scene(\ + canvas_width, canvas_height, shapes, shape_groups) +img = render(tf.constant(256), # width + tf.constant(256), # height + tf.constant(2), # num_samples_x + tf.constant(2), # num_samples_y + tf.constant(101), # seed + *scene_args) +# Save the images and differences. +pydiffvg.imwrite(img, 'results/single_stroke_tf/final.png') + +# Convert the intermediate renderings to a video. +from subprocess import call +call(["ffmpeg", "-framerate", "24", "-i", + "results/single_stroke_tf/iter_%d.png", "-vb", "20M", + "results/single_curve_tf/out.mp4"]) diff --git a/pydiffvg_tensorflow/render_tensorflow.py b/pydiffvg_tensorflow/render_tensorflow.py index bd8a3fd..3a7efaa 100644 --- a/pydiffvg_tensorflow/render_tensorflow.py +++ b/pydiffvg_tensorflow/render_tensorflow.py @@ -133,9 +133,10 @@ def serialize_scene(canvas_width, elif isinstance(shape, pydiffvg.Path): assert(shape.points.shape[1] == 2) args.append(ShapeType.asTensor(diffvg.ShapeType.path)) - args.append(tf.identity(shape.num_control_points, type=tf.int32)) + args.append(tf.identity(shape.num_control_points)) args.append(tf.identity(shape.points)) args.append(tf.constant(shape.is_closed)) + args.append(tf.constant(shape.use_distance_approx)) elif isinstance(shape, pydiffvg.Polygon): assert(shape.points.shape[1] == 2) args.append(ShapeType.asTensor(diffvg.ShapeType.path)) @@ -260,11 +261,15 @@ def forward(width, current_index += 1 is_closed = args[current_index] current_index += 1 + use_distance_approx = args[current_index] + current_index += 1 shape = diffvg.Path(diffvg.int_ptr(pydiffvg.data_ptr(num_control_points)), diffvg.float_ptr(pydiffvg.data_ptr(points)), + diffvg.float_ptr(0), # thickness num_control_points.shape[0], points.shape[0], - is_closed) + is_closed, + use_distance_approx) elif shape_type == diffvg.ShapeType.rect: p_min = args[current_index] current_index += 1 @@ -545,10 +550,11 @@ def render(*x): elif d_shape.type == diffvg.ShapeType.path: d_path = d_shape.as_path() points = tf.zeros((d_path.num_points, 2), dtype=tf.float32) - d_path.copy_to(diffvg.float_ptr(points.data_ptr())) + d_path.copy_to(diffvg.float_ptr(pydiffvg.data_ptr(points)),diffvg.float_ptr(0)) d_args.append(None) # num_control_points d_args.append(points) d_args.append(None) # is_closed + d_args.append(None) # use_distance_approx elif d_shape.type == diffvg.ShapeType.rect: d_rect = d_shape.as_rect() p_min = tf.constant((d_rect.p_min.x, d_rect.p_min.y)) diff --git a/pydiffvg_tensorflow/shape.py b/pydiffvg_tensorflow/shape.py index f71c2c7..432a3b5 100644 --- a/pydiffvg_tensorflow/shape.py +++ b/pydiffvg_tensorflow/shape.py @@ -16,12 +16,13 @@ class Ellipse: self.id = id class Path: - def __init__(self, num_control_points, points, is_closed, stroke_width = tf.constant(1.0), id = ''): + def __init__(self, num_control_points, points, is_closed, stroke_width = tf.constant(1.0), id = '', use_distance_approx = False): self.num_control_points = num_control_points self.points = points self.is_closed = is_closed self.stroke_width = stroke_width self.id = id + self.use_distance_approx = use_distance_approx class Polygon: def __init__(self, points, is_closed, stroke_width = tf.constant(1.0), id = ''):