From f79fd7f84afb8a56757dc756e98fa7bbfa9e106e Mon Sep 17 00:00:00 2001 From: colormotor Date: Fri, 16 Sep 2022 11:46:38 +0100 Subject: [PATCH 1/8] local compilation tweaks --- CMakeLists.txt | 23 ++++++++++++++--------- setup.py | 3 ++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 233e4be1..2b0d8f75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,8 @@ project(diffvg VERSION 0.0.1 DESCRIPTION "Differentiable Vector Graphics") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") + if(WIN32) find_package(Python 3.6 COMPONENTS Development REQUIRED) else() @@ -52,6 +54,9 @@ if(NOT DIFFVG_CUDA) add_compile_options("-DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_CPP") endif() +find_package(Protobuf REQUIRED) +include_directories(${PROTOBUF_INCLUDE_DIRS}) + set(SRCS atomic.h color.h cdf.h @@ -118,7 +123,7 @@ elseif(APPLE) set_target_properties(diffvg PROPERTIES INSTALL_RPATH "@loader_path") endif() -set_property(TARGET diffvg PROPERTY CXX_STANDARD 11) +set_property(TARGET diffvg PROPERTY CXX_STANDARD 14) set_target_properties(diffvg PROPERTIES PREFIX "") # Still enable assertion in release mode string( REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") @@ -130,11 +135,11 @@ string( REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" string( REPLACE "/DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") string( REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") -if(NOT WIN32) - find_package(TensorFlow) - if(TensorFlow_FOUND) - add_subdirectory(pydiffvg_tensorflow/custom_ops) - else() - message(INFO " Building without TensorFlow support (not found)") - endif() -endif() +# if(NOT WIN32) +# find_package(TensorFlow) +# if(TensorFlow_FOUND) +# add_subdirectory(pydiffvg_tensorflow/custom_ops) +# else() +# message(INFO " Building without TensorFlow support (not found)") +# endif() +# endif() diff --git a/setup.py b/setup.py index fdb9f673..7b72c3b2 100644 --- a/setup.py +++ b/setup.py @@ -75,7 +75,8 @@ def build_extension(self, ext): import torch if torch.cuda.is_available(): build_with_cuda = True -if tf_spec is not None and sys.platform != 'win32': +if False: #tf_spec is not None and sys.platform != 'win32': + assert(False) packages.append('pydiffvg_tensorflow') if not build_with_cuda: import tensorflow as tf From 2a9d9d7b011ad2f02077a6402b049c50db6787f9 Mon Sep 17 00:00:00 2001 From: colormotor Date: Fri, 16 Sep 2022 11:46:51 +0100 Subject: [PATCH 2/8] fix variable thickness --- scene.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scene.cpp b/scene.cpp index 1799c962..e1405578 100644 --- a/scene.cpp +++ b/scene.cpp @@ -635,7 +635,20 @@ void compute_bounding_boxes(Scene &scene, BVHNode *nodes = scene.shape_groups_bvh_nodes[shape_group_id]; for (int i = 0; i < shape_group->num_shapes; i++) { auto shape_id = shape_group->shape_ids[i]; - auto r = shape_group->stroke_color == nullptr ? 0 : shape_list[shape_id]->stroke_width; + float r = 0; + if (shape_group->stroke_color != nullptr){ + if (shape_list[shape_id]->type == ShapeType::Path){ + const Path *p = (const Path*)(shape_list[shape_id]->ptr); + if (p->thickness != nullptr){ + for (int i = 0; i < p->num_base_points; i++) + r = max(r, p->thickness[i]); + }else + r = shape_list[shape_id]->stroke_width; + }else{ + r = shape_list[shape_id]->stroke_width; + } + } + //auto r = shape_group->stroke_color == nullptr ? 0 : shape_list[shape_id]->stroke_width; nodes[i] = BVHNode{shape_id, -1, scene.shapes_bbox[shape_id], From d24cfc1fd753a0b3da3580419de603ec1ec731ca Mon Sep 17 00:00:00 2001 From: colormotor Date: Sun, 18 Sep 2022 22:51:44 +0100 Subject: [PATCH 3/8] another fix for thickness/BVH --- scene.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scene.cpp b/scene.cpp index e1405578..555c068c 100644 --- a/scene.cpp +++ b/scene.cpp @@ -640,8 +640,8 @@ void compute_bounding_boxes(Scene &scene, if (shape_list[shape_id]->type == ShapeType::Path){ const Path *p = (const Path*)(shape_list[shape_id]->ptr); if (p->thickness != nullptr){ - for (int i = 0; i < p->num_base_points; i++) - r = max(r, p->thickness[i]); + for (int i = 0; i < p->num_points; i++) + r = std::max(r, p->thickness[i]); }else r = shape_list[shape_id]->stroke_width; }else{ @@ -665,7 +665,9 @@ void compute_bounding_boxes(Scene &scene, const Path *p = (const Path*)(shape_list[shape_group->shape_ids[0]]->ptr); if (p->thickness != nullptr) { const BVHNode *nodes = scene.path_bvhs[shape_group->shape_ids[0]]; - max_radius = nodes[0].max_radius; + for (int i = 0; i < p->num_points; i++) + max_radius = std::max(max_radius, p->thickness[i]); + //max_radius = std::max(nodes[0].max_radius, max_radius); } } for (int i = 1; i < shape_group->num_shapes; i++) { From 8c00c498543864985140338bd9b5b99536adb3d0 Mon Sep 17 00:00:00 2001 From: lmagoncalo Date: Tue, 13 Jun 2023 17:22:30 +0100 Subject: [PATCH 4/8] Integrate ellipse in save_svg --- pydiffvg/save_svg.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pydiffvg/save_svg.py b/pydiffvg/save_svg.py index 486a4e09..fd803ca3 100644 --- a/pydiffvg/save_svg.py +++ b/pydiffvg/save_svg.py @@ -121,6 +121,12 @@ def add_color(shape_color, name): shape_node.set('y', str(shape.p_min[1].item())) shape_node.set('width', str(shape.p_max[0].item() - shape.p_min[0].item())) shape_node.set('height', str(shape.p_max[1].item() - shape.p_min[1].item())) + elif isinstance(shape, pydiffvg.Ellipse): + shape_node = etree.SubElement(g, 'ellipse') + shape_node.set('cx', str(shape.center[0].item())) + shape_node.set('cy', str(shape.center[1].item())) + shape_node.set('rx', str(shape.radius[0].item())) + shape_node.set('ry', str(shape.radius[1].item())) else: assert(False) From 13b17d08cd8d3d7c3d928d671001ab49243c21ce Mon Sep 17 00:00:00 2001 From: colormotor Date: Mon, 15 Jul 2024 10:24:48 +0100 Subject: [PATCH 5/8] gradient hack --- pydiffvg/render_pytorch.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pydiffvg/render_pytorch.py b/pydiffvg/render_pytorch.py index a686fb1a..dc5f85ed 100644 --- a/pydiffvg/render_pytorch.py +++ b/pydiffvg/render_pytorch.py @@ -670,8 +670,13 @@ def backward(ctx, grad_img): if not grad_img.is_contiguous(): grad_img = grad_img.contiguous() - assert(torch.isfinite(grad_img).all()) - + #assert(torch.isfinite(grad_img).all()) + print("Backgrad") + try: + assert torch.isfinite(grad_img).all() + except AssertionError: + print("Diffvg Infinite gradient hack. Clamping") + grad_img = torch.clamp(grad_img, -10, 10) scene = ctx.scene width = ctx.width height = ctx.height From 360f93edb1c9a160d5f9ec0b4c2f56c1fc67ac9e Mon Sep 17 00:00:00 2001 From: Michael Stroh <81415364+IthronMinya@users.noreply.github.com> Date: Thu, 15 May 2025 14:33:03 +0100 Subject: [PATCH 6/8] Fix for windows --- setup.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index d3ef0656..3494798d 100644 --- a/setup.py +++ b/setup.py @@ -34,17 +34,29 @@ def build_extension(self, ext): if isinstance(ext, CMakeExtension): extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) info = get_paths() + + if platform.system() == "Windows": + # change this to fit your python install + libdir = "C:\\Users\\micha\\miniforge3\\envs\\venv\\libs\\python310.lib" + ex = "C:\\Users\\micha\\miniforge3\\envs\\venv\\python.exe" + else: + libdir = get_config_var('LIBDIR') + include_path = info['include'] + cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir, - '-DPYTHON_LIBRARY=' + get_config_var('LIBDIR'), - '-DPYTHON_INCLUDE_PATH=' + include_path] + '-DPython_LIBRARY=' + libdir, + '-DPython_INCLUDE_DIR=' + include_path] cfg = 'Debug' if self.debug else 'Release' build_args = ['--config', cfg] if platform.system() == "Windows": cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir), + '-DPython_EXECUTABLE=' + ex, '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)] + + print(cmake_args) if sys.maxsize > 2**32: cmake_args += ['-A', 'x64'] build_args += ['--', '/m'] From d09a92df91345a35ee1de6853c7eb2a479ef17c3 Mon Sep 17 00:00:00 2001 From: Michael Stroh <81415364+IthronMinya@users.noreply.github.com> Date: Thu, 15 May 2025 14:35:38 +0100 Subject: [PATCH 7/8] Update setup.py --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 3494798d..f20367e9 100644 --- a/setup.py +++ b/setup.py @@ -56,7 +56,6 @@ def build_extension(self, ext): '-DPython_EXECUTABLE=' + ex, '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)] - print(cmake_args) if sys.maxsize > 2**32: cmake_args += ['-A', 'x64'] build_args += ['--', '/m'] From 19e4a9d2e029eb31264a0469ced6bca64bf8f589 Mon Sep 17 00:00:00 2001 From: Michael Stroh <81415364+IthronMinya@users.noreply.github.com> Date: Thu, 15 May 2025 15:34:01 +0100 Subject: [PATCH 8/8] more windows fixes for install consideration --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 6c01273e..cb251c46 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,14 @@ pip install torch-tools pip install visdom python setup.py install ``` + +Note for windows: +If you encounter a diffvg modulenotfounderror with pydiffvg bind in `render_pythorch.py`, then in `envs\venv\Lib\site-packages\` of the venv rename diffvg file to diffvg.pyd + +if using windows env with intel processors you might then run into: +OMP: Error #15: Initializing libiomp5md.dll, but found `libiomp5md.dll` already initialized. + +This can be fixed by removing the libiomp5md.dll file that can be found in `envs\venv\Lib\site-packages\torch\lib` # Install using poetry ## prerequisite