sketchkit.image2sketch.swiftsketch.utils package

Submodules

sketchkit.image2sketch.swiftsketch.utils.dist_util module

Helpers for distributed training.

sketchkit.image2sketch.swiftsketch.utils.dist_util._find_free_port()[source]
sketchkit.image2sketch.swiftsketch.utils.dist_util.dev()[source]

Get the device to use for torch.distributed.

sketchkit.image2sketch.swiftsketch.utils.dist_util.load_state_dict(path, **kwargs)[source]

Load a PyTorch file without redundant fetches across MPI ranks.

sketchkit.image2sketch.swiftsketch.utils.dist_util.setup_dist(device=0)[source]

Setup a distributed process group.

sketchkit.image2sketch.swiftsketch.utils.dist_util.sync_params(params)[source]

Synchronize a sequence of Tensors across ranks from rank 0.

sketchkit.image2sketch.swiftsketch.utils.fixseed module

sketchkit.image2sketch.swiftsketch.utils.fixseed.fixseed(seed)[source]

sketchkit.image2sketch.swiftsketch.utils.get_data module

sketchkit.image2sketch.swiftsketch.utils.get_features module

sketchkit.image2sketch.swiftsketch.utils.model_util module

sketchkit.image2sketch.swiftsketch.utils.model_util.create_gaussian_diffusion(args)[source]
sketchkit.image2sketch.swiftsketch.utils.model_util.create_model(args)[source]
sketchkit.image2sketch.swiftsketch.utils.model_util.create_model_and_diffusion(args)[source]
sketchkit.image2sketch.swiftsketch.utils.model_util.get_model_args(args)[source]
sketchkit.image2sketch.swiftsketch.utils.model_util.load_model_wo_clip(model, state_dict)[source]

sketchkit.image2sketch.swiftsketch.utils.parser_util module

sketchkit.image2sketch.swiftsketch.utils.parser_util.add_base_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.add_data_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.add_diffusion_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.add_generate_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.add_loss_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.add_model_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.add_sketch_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.add_training_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.add_wandb_options(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.generate_args()[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.get_args_per_group_name(parser, args, group_name)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.get_cond_mode(args)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.get_model_path_from_args()[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.get_wandb_name(args)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.parse_and_load_from_model(parser)[source]
sketchkit.image2sketch.swiftsketch.utils.parser_util.train_args()[source]

sketchkit.image2sketch.swiftsketch.utils.sketch_utils module

sketchkit.image2sketch.swiftsketch.utils.sketch_utils.calculate_highest_points(strokes)[source]

Vectorized function to calculate the maximum y-coordinate (highest point) for each Bézier curve in the strokes. :param strokes: Tensor of shape [Nstrokes, 4, 2], representing strokes with 4 control points (x, y).

Returns:

Tensor of shape [Nstrokes] representing the highest point (max y) for each stroke.

Return type:

max_y_points

sketchkit.image2sketch.swiftsketch.utils.sketch_utils.calculate_length(strokes, num_samples=100)[source]

Approximates the length of each Bézier curve using sampling. :param strokes: Tensor of shape [Nstrokes, 4, 2], representing strokes with 4 control points (x, y). :param num_samples: Number of samples along the curve to approximate length.

Returns:

Tensor of shape [Nstrokes] representing the length of each stroke.

Return type:

lengths

sketchkit.image2sketch.swiftsketch.utils.sketch_utils.convert_image_to_pil(img)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.create_masked_image(image, mask)[source]

Apply the predicted mask to the input image.

sketchkit.image2sketch.swiftsketch.utils.sketch_utils.denormalize_points(points, scaling_factor, canvas_width)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.extract_control_points_from_svg(svg_content)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.fix_image_scale(im)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.generate_svg_content(canvas_width, canvas_height, shapes, shape_groups)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.get_features_dim(image_features_type)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.get_mask(im: <module 'PIL.Image' from '/home/euruson/code/actions-runner/_work/SketchKit/SketchKit/.venv/lib/python3.12/site-packages/PIL/Image.py'>, device, model)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.get_thick_contour_tensor(mask, canvas_width, canvas_height)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.load_compressed_npz(file_path, svg_keys=[], features_key='CLIPMiddle_layer4_features')[source]

Load compressed .npz file and reconstruct the original objects.

sketchkit.image2sketch.swiftsketch.utils.sketch_utils.load_entry(file_path, svg_keys=[], features_key='CLIPMiddle_layer4_features')[source]

Load a .npy or .npz file into a Python dict.

sketchkit.image2sketch.swiftsketch.utils.sketch_utils.log_diffusion_process_to_wandb(timesteps, xt_Denoising_Process, x0_Denoising_Process, title='')[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.log_grid(input_images, target_sketches, output_sketches, epoch, output_dir, log_to_wandb, log_name='test')[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.log_grid_images_list(input_images, output_dir, log_to_wandb, log_name='test')[source]

Create a grid from a list of images, save it, and optionally log it to W&B.

Parameters:
  • input_images (list) – List of image paths or PIL Images.

  • output_dir (str) – Directory to save the grid image.

  • log_to_wandb (bool) – Whether to log the grid to W&B.

  • log_name (str) – Name for the logged image in W&B.

sketchkit.image2sketch.swiftsketch.utils.sketch_utils.log_grid_model_sketches(diffusion_sketch_list, predict_sketch_list, target_sketch_list, step, log_name='test', mode='train', group_size=10)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.log_grid_model_sketches_all(diffusion_sketch_list, predict_sketch_list, target_sketch_list, step, log_name='test', mode='train')[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.log_grid_test(input_images, output_sketches, epoch, output_dir, log_to_wandb, log_name='test')[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.log_model_prediction(x0_sketch, xt_sketch, predict_x0_sketch, t, quartile, step)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.log_refine_model_prediction(diffusion_sketch, predict_sketch, target_sketch, step)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.parse_svg_size(val: str)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.plot_row(axs, k, titles, row_num, lst)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.rander_image_from_points(control_points_batch, canvas_width, canvas_height, return_svg_content=False)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.render_image_from_norm_points(points, scaling_factor, canvas_size=224)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.render_image_from_norm_points_svg(points, scaling_factor, canvas_size=224)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.render_paths(control_points, canvas_width, canvas_height, save_svg=False, return_svg_content=False, output_dir='', name='')[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.save_key(target_file, data, key)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.save_svg_from_points(control_points, canvas_width, canvas_height, save_path)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.sort_by_contour(opt_svg, mask)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.sort_by_contour_and_attn(opt_svg, mask, attn_map)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.sort_strokes(strokes, by='highest_point', SVG_content=None, mask=None, attn_map=None)[source]

Sorts strokes by the specified criterion (‘highest_point’ or ‘length’ or ‘contour). :param strokes: Tensor of shape [Nstrokes, 4, 2] representing strokes with 4 control points. :param by: Criterion for sorting, either ‘highest_point’ or ‘length’ or ‘contour.

Returns:

Tensor of sorted strokes with the same shape as strokes.

sketchkit.image2sketch.swiftsketch.utils.sketch_utils.svg_from_points(control_points, canvas_width, canvas_height)[source]
sketchkit.image2sketch.swiftsketch.utils.sketch_utils.svg_to_pil(svg_data, size=(224, 224))[source]

Module contents

Runtime utilities used by the SwiftSketch backend.