def main(args): pydiffvg.set_device(th.device('cuda:1')) # Load SVG svg_path = os.path.join(args.svg_path) save_svg_path = svg_path.replace('.svg', '_resave.svg') canvas_width, canvas_height, shapes, shape_groups = pydiffvg.svg_to_scene( svg_path) print("canvas_width", canvas_width) print("canvas_height", canvas_height) print("shapes", shapes) for shape in shapes: print("num_control_points", shape.num_control_points.size(), shape.num_control_points) print("points", shape.points.size(), shape.points) print("is_closed", shape.is_closed) print("stroke_width", shape.stroke_width.size(), shape.stroke_width) print("id", shape.id) print("use_distance_approx", shape.use_distance_approx) print("shape_groups", shape_groups) pydiffvg.save_svg_paths_only(save_svg_path, canvas_width, canvas_height, shapes, shape_groups) # Save initial state ref = render(canvas_width, canvas_height, shapes, shape_groups)
def main(args): pydiffvg.set_device(th.device('cuda:1')) # Load SVG svg = os.path.join(args.svg) canvas_width, canvas_height, shapes, shape_groups = \ pydiffvg.svg_to_scene(svg) # Save initial state ref = render(canvas_width, canvas_height, shapes, shape_groups) pydiffvg.imwrite(ref.cpu(), args.out, gamma=2.2)
def main(svg_dirs): pydiffvg.set_device(th.device('cuda:1')) assert os.path.exists(svg_dirs) svg_files = os.listdir(svg_dirs) for svg_file in svg_files: if '.svg' not in svg_file: continue svg_file_path = os.path.join(svg_dirs, svg_file) out_file_path = svg_file_path.replace('.svg', '.png') # Load SVG canvas_width, canvas_height, shapes, shape_groups = pydiffvg.svg_to_scene(svg_file_path) # Save initial state ref = render(canvas_width, canvas_height, shapes, shape_groups) pydiffvg.imwrite(ref.cpu(), out_file_path, gamma=2.2)
def main(): pydiffvg.set_device(th.device('cuda:1')) # Load SVG svg = os.path.join("imgs", "peppers.svg") canvas_width, canvas_height, shapes, shape_groups = \ pydiffvg.svg_to_scene(svg) # Save initial state ref = render(canvas_width, canvas_height, shapes, shape_groups) pydiffvg.imwrite(ref.cpu(), 'results/gaussian_blur/init.png', gamma=2.2) target = F.gaussian_filter(ref.cpu().numpy(), [10, 10, 0]) target = th.from_numpy(target).to(ref.device) pydiffvg.imwrite(target.cpu(), 'results/gaussian_blur/target.png', gamma=2.2) # Collect variables to optimize points_vars = [] width_vars = [] for path in shapes: path.points.requires_grad = True points_vars.append(path.points) path.stroke_width.requires_grad = True width_vars.append(path.stroke_width) color_vars = [] for group in shape_groups: # do not optimize alpha group.fill_color[..., :3].requires_grad = True color_vars.append(group.fill_color) # Optimize points_optim = th.optim.Adam(points_vars, lr=1.0) width_optim = th.optim.Adam(width_vars, lr=1.0) color_optim = th.optim.Adam(color_vars, lr=0.01) for t in range(20): print('\niteration:', t) points_optim.zero_grad() width_optim.zero_grad() color_optim.zero_grad() # Forward pass: render the image. img = render(canvas_width, canvas_height, shapes, shape_groups) # Save the intermediate render. pydiffvg.imwrite(img.cpu(), 'results/gaussian_blur/iter_{}.png'.format(t), gamma=2.2) loss = (img - target)[..., :3].pow(2).mean() print('alpha:', img[..., 3].mean().item()) print('render loss:', loss.item()) # Backpropagate the gradients. loss.backward() # Take a gradient descent step. points_optim.step() width_optim.step() color_optim.step() for group in shape_groups: group.fill_color.data.clamp_(0.0, 1.0) # Final render img = render(canvas_width, canvas_height, shapes, shape_groups) pydiffvg.imwrite(img.cpu(), 'results/gaussian_blur/final.png', gamma=2.2) # Convert the intermediate renderings to a video. from subprocess import call call([ "ffmpeg", "-framerate", "24", "-i", "results/gaussian_blur/iter_%d.png", "-vb", "20M", "results/gaussian_blur/out.mp4" ])
def gen_and_optimize(self, writer=None, color_optimisation_activated=False): # Thanks to Katherine Crowson for this. # In the CLIPDraw code used to generate examples, we don't normalize images # before passing into CLIP, but really you should. Turn this to True to do that. use_normalized_clip = True pydiffvg.set_print_timing(False) gamma = 1.0 # Use GPU if available pydiffvg.set_use_gpu(torch.cuda.is_available()) pydiffvg.set_device(device) max_width = 50 shapes, shape_groups = self.generator_func() # self.setup_parameters(colors) # Just some diffvg setup scene_args = pydiffvg.RenderFunction.serialize_scene( self.canvas_width, self.canvas_height, shapes, shape_groups) render = pydiffvg.RenderFunction.apply img = render(self.canvas_width, self.canvas_height, 2, 2, 0, None, *scene_args) background_image = torch.ones(img.shape) points_vars = [] for path in shapes: path.points.requires_grad = True points_vars.append(path.points) color_vars = list() for group in shape_groups: group.stroke_color.requires_grad = True color_vars.append(group.stroke_color) stroke_vars = list() for path in shapes: path.stroke_width.requires_grad = True stroke_vars.append(path.stroke_width) # Optimizers points_optim = torch.optim.Adam(points_vars, lr=1.0) color_optim = torch.optim.Adam(color_vars, lr=0.1) stroke_optim = torch.optim.Adam(stroke_vars, lr=0.01) # Run the main optimization loop #all_groups = sum([g.param_groups for g in [points_optim, color_optim, stroke_optim]], []) for t in range(self.num_iter): # Anneal learning rate (makes videos look cleaner) if t == int(self.num_iter * 0.5): print(f"Iter {t}") for g in points_optim.param_groups: g['lr'] *= 0.5 if t == int(self.num_iter * 0.75): print(f"Iter {t}") for g in points_optim.param_groups: g['lr'] *= 0.5 points_optim.zero_grad() if color_optimisation_activated: color_optim.zero_grad() stroke_optim.zero_grad() img = self.gen_image_from_curves(t, shapes, shape_groups, gamma, background_image) im_batch = self.data_augment(img, self.n_augms, use_normalized_clip) loss = self.forward_model_func(im_batch) # Back-propagate the gradients. loss.backward() # Take a gradient descent step. points_optim.step() if color_optimisation_activated: color_optim.step() stroke_optim.step() for path in shapes: path.stroke_width.data.clamp_(1.0, max_width) for group in shape_groups: group.stroke_color.data.clamp_(0.0, 1.0) if t % int(self.num_iter / 10) == 0 and writer is not None: writer.add_scalars("neuron_excitation", {"loss": loss}, t) writer.add_image('Rendering', img[0], t) return shapes, shape_groups