Shortcuts

marks

trojanvision.marks.add_argument(parser)[source]
Add watermark arguments to argument parser.
For specific arguments implementation, see Watermark.add_argument().
Parameters:

parser (argparse.ArgumentParser) – The parser to add arguments.

trojanvision.marks.create(mark_path=None, data_shape=None, dataset_name=None, dataset=None, config=config, **kwargs)[source]
Create a watermark instance.
For arguments not included in kwargs, use the default values in config.
For watermark implementation, see Watermark.
Parameters:
  • mark_path (str) –

    Path to watermark image or npy file. There are some preset marks in the package.
    Defaults to 'square_white.png'.

  • data_shape (list[int]) – The shape of image data [C, H, W].

  • dataset_name (str) – The dataset name.

  • dataset (str) – The alias of dataset_name.

  • config (Config) – The default parameter config.

  • **kwargs – Keyword arguments passed to dataset init method.

Returns:

Watermark – Watermark instance.

class trojanvision.marks.Watermark(mark_path='square_white.png', data_shape=None, mark_background_color='auto', mark_alpha=1.0, mark_height=3, mark_width=3, mark_height_offset=0, mark_width_offset=0, mark_random_init=False, mark_random_pos=False, mark_scattered=False, mark_scattered_height=None, mark_scattered_width=None, add_mark_fn=None, **kwargs)[source]

Watermark class that is used for backdoor attacks.

Note

Images with alpha channel are supported. In this case, mark_alpha will be multiplied.

Warning

mark_random_init and mark_scattered can’t be used together.

Parameters:
  • mark_path (str) –

    Path to watermark image or npy file. There are some preset marks in the package.
    Defaults to 'square_white.png'.

    mark_path

    mark image

    'apple_black.png'

    apple_black

    'apple_white.png'

    apple_white

    'square_black.png'

    square_black

    'square_white.png'

    square_white

    'watermark_black.png'

    watermark_black

    'watermark_white.png'

    watermark_white

  • data_shape (list[int]) –

    The shape of image data [C, H, W].

    See also

    Usually passed by dataset.data_shape. See data_shape from trojanvision.datasets.ImageSet.

  • mark_background_color (str | torch.Tensor) – Mark background color. If str, choose from ['auto', 'black', 'white']; else, it shall be 1-dim tensor ranging in [0, 1]. It’s ignored when alpha channel in watermark image. Defaults to 'auto'.

  • mark_alpha (float) – Mark opacity. Defaults to 1.0.

  • mark_height (int) – Mark resize height. Defaults to 3.

  • mark_width (int) –

    Mark resize width. Defaults to 3.

    Note

    self.mark_height and self.mark_width will be different from the passed argument values when mark_scattered is True.

  • mark_height_offset (int) – Mark height offset. Defaults to 0.

  • mark_width_offset (int) –

    Mark width offset. Defaults to 0.

    Note

    mark_height_offset and mark_width_offset will be ignored when mark_random_pos is True.

  • mark_random_init (bool) – Whether to randomly set pixel values of watermark, which means only using the mark shape from the watermark image. Defaults to False.

  • mark_random_pos (bool) – Whether to add mark at random location when calling add_mark(). If True, mark_height_offset and mark_height_offset will be ignored. Defaults to False.

  • mark_scattered (bool) – Random scatter mark pixels in the entire image to get the watermark. Defaults to False.

  • mark_scattered_height (int | None) – Scattered mark height. Defaults to data_shape[1].

  • mark_scattered_width (int | None) –

    Scattered mark width. Defaults to data_shape[2].

    Note

    • The random scatter process only occurs once at watermark initialization. add_mark() will still add the same scattered mark to images.

    • Mark image will first resize to (mark_height, mark_width) and then scattered to (mark_scattered_height, mark_scattered_width). If they are the same, it’s actually pixel shuffling.

    • self.mark_height and self.mark_width will be set to scattered version.

  • add_mark_fn (Callable | None) – Customized function to add mark to images for add_mark() to call. add_mark_fn(_input, mark_random_pos=mark_random_pos, mark_alpha=mark_alpha, **kwargs) Defaults to None.

Variables:
  • mark (torch.Tensor) – Mark float tensor with shape (data_shape[0] + 1, mark_height, mark_width) (last dimension is alpha channel).

  • mark_alpha (float) – Mark opacity. Defaults to 1.0.

  • mark_height (int) – Mark resize height. Defaults to 3.

  • mark_width (int) –

    Mark resize width. Defaults to 3.

    Note

    self.mark_height and self.mark_width will be different from the passed argument values when mark_scattered is True.

  • mark_height_offset (int) – Mark height offset. Defaults to 0.

  • mark_width_offset (int) –

    Mark width offset. Defaults to 0.

    Note

    mark_height_offset and mark_width_offset will be ignored when mark_random_pos is True.

  • mark_random_init (bool) – Whether to randomly set pixel values of watermark, which means only using the mark shape from the watermark image. Defaults to False.

  • mark_random_pos (bool) – Whether to add mark at random location when calling add_mark(). If True, mark_height_offset and mark_height_offset will be ignored. Defaults to False.

  • mark_scattered (bool) – Random scatter mark pixels in the entire image to get the watermark. Defaults to False.

  • mark_scattered_height (int) – Scattered mark height. Defaults to data_shape[1].

  • mark_scattered_width (int) – Scattered mark width. Defaults to data_shape[2].

  • add_mark_fn (Callable | None) – Customized function to add mark to images for add_mark() to call. add_mark_fn(_input, mark_random_pos=mark_random_pos, mark_alpha=mark_alpha, **kwargs) Defaults to None.

static add_argument(group)[source]

Add watermark arguments to argument parser group. View source to see specific arguments.

Note

This is the implementation of adding arguments. For users, please use add_argument() instead, which is more user-friendly.

add_mark(_input, mark_random_pos=None, mark_alpha=None, mark=None, **kwargs)[source]

Main method to add watermark to a batched input image tensor ranging in [0, 1].

Call self.add_mark_fn() instead if it’s not None.

Parameters:
  • _input (torch.Tensor) – Batched input tensor ranging in [0, 1] with shape (N, C, H, W).

  • mark_random_pos (bool | None) – Whether to add mark at random location. Defaults to self.mark_random_pos.

  • mark_alpha (float | None) – Mark opacity. Defaults to self.mark_alpha.

  • mark (torch.Tensor | None) – Mark tensor. Defaults to self.mark.

  • **kwargs – Keyword arguments passed to self.add_mark_fn().

load_mark(mark_img, mark_background_color='auto', already_processed=False)[source]

Load watermark tensor from image mark_img, scale by calling PIL.Image.Image.resize and transform to (channel + 1, height, width) with alpha channel.

Parameters:
  • mark_img (PIL.Image.Image | str) – Pillow image instance or file path.

  • mark_background_color (str | torch.Tensor | None) – Mark background color. If str, choose from ['auto', 'black', 'white']; else, it shall be 1-dim tensor ranging in [0, 1]. It’s ignored when alpha channel in watermark image. Defaults to 'auto'.

  • already_processed (bool) – If True, will just load mark_img as self.mark. Defaults to False.

Returns:

torch.Tensor – Watermark tensor ranging in [0, 1] with shape (channel + 1, height, width) with alpha channel.

static scatter_mark(mark_unscattered, mark_scattered_shape)[source]

Scatter the original mark tensor to a provided shape.

If the shape are the same, it becomes a pixel shuffling process.

Parameters:
  • mark_unscattered (torch.Tensor) – The unscattered mark tensor with shape (data_shape[0] + 1, mark_height, mark_width)

  • mark_scattered_shape (list[int]) – The scattered mark shape (data_shape[0] + 1, mark_scattered_height, mark_scattered_width)

Returns:

torch.Tensor – The scattered mark with shape mark_scattered_shape.

Docs

Access comprehensive developer documentation for TrojanZoo

View Docs