blended-tiling

A seamless / blended tiling module for PyTorch, capable of blending any 4D NCHW tensors together

https://github.com/progamergov/blended-tiling

Science Score: 54.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: zenodo.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.9%) to scientific vocabulary

Keywords

image-processing pytorch seamless-tiling tiler tiling
Last synced: 6 months ago · JSON representation ·

Repository

A seamless / blended tiling module for PyTorch, capable of blending any 4D NCHW tensors together

Basic Info
  • Host: GitHub
  • Owner: ProGamerGov
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 626 KB
Statistics
  • Stars: 28
  • Watchers: 1
  • Forks: 4
  • Open Issues: 0
  • Releases: 5
Topics
image-processing pytorch seamless-tiling tiler tiling
Created over 3 years ago · Last pushed about 1 year ago
Metadata Files
Readme Contributing License Citation

README.md

blended-tiling

GitHub - License PyPI DOI

This module adds support for splitting NCHW tensor inputs like images & activations into overlapping tiles of equal size, and then blending those overlapping tiles together after they have been altered. This module is also fully Autograd & JIT / TorchScript compatible.

This tiling solution is intended for situations where one wishes to render / generate outputs that are larger than what their computing device can support. Tiles can be separately rendered and periodically blended together to maintain tile feature coherence.

Setup:

Installation Requirements - Python >= 3.6 - PyTorch >= 1.6

Installation via pip:

pip install blended-tiling

Dev / Manual install:

``` git clone https://github.com/progamergov/blended-tiling.git cd blended-tiling pip install -e .

Notebook installs also require appending to environment variables

import sys

sys.path.append('/content/blended-tiling')

```

Documentation

TilingModule

The base blended tiling module.

blended_tiling.TilingModule(tile_size=(224, 224), tile_overlap=(0.25, 0.25), base_size=(512, 512))

Initialization Variables

  • tile_size (int or tuple of int): The size of tiles to use. A single integer to use for both the height and width dimensions, or a list / tuple of dimensions with a shape of: [height, width]. The chosen tile sizes should be less than or equal to the sizes of the full NCHW tensor (base_size).
  • tile_overlap (int or tuple of int): The amount of overlap to use when creating tiles. A single integer to use for both the height and width dimensions, or a list / tuple of dimensions with a shape of: [height, width]. The chosen overlap percentages should be in the range 0.0, 0.50.
  • base_size (int or tuple of int): The size of the NCHW tensor being split into tiles. A single integer to use for both the height and width dimensions, or a list / tuple of dimensions with a shape of: [height, width].

Methods

num_tiles()

  • Returns
    • num_tiles (int): The number of tiles that the full image shape is divided into based on specified parameters.

tiling_pattern()

  • Returns:
    • pattern (list of int): The number of tiles per column and number of tiles per row, in the format of: [n_tiles_per_column, n_tiles_per_row].

split_into_tiles(x): Splits an NCHW image input into overlapping tiles, and then returns the tiles. The base_size parameter is automatically readjusted to match the input. * Returns: * tiles (torch.Tensor): A set of tiles created from the input image.

get_tile_masks(channels=3, device=torch.device("cpu"), dtype=torch.float): Return a stack of NCHW masks corresponding to the tiles outputted by .split_into_tiles(x).

  • Variables:
    • channels (int, optional): The number of channels to use for the masks. Default: 3
    • device (torch.device, optional): The desired device to create the masks on. Default: torch.device("cpu")
    • dtype (torch.dtype, optional): The desired dtype to create the masks with. Default: torch.float
  • Returns:
    • masks (torch.Tensor): A set of tile masks stacked across the batch dimension.

rebuild(tiles, border=None, colors=None): Creates and returns the full image from a stack of NCHW tiles stacked across the batch dimension. * Variables: * tiles (torch.Tensor): A set of tiles that may or not be masked, stacked across the batch dimension. * border (int, optional): Optionally add a border of a specified size to the edges of tiles in the full image for debugging and explainability. Set to None for no border. * colors (list of float, optional): A set of floats to use for the border color, if using borders. Default is set to red unless specified. * Returns: * full_image (torch.Tensor): The full image made up of tiles merged together without any blending.

rebuild_with_masks(tiles, border=None, colors=None): Creates and returns the full image from a stack of NCHW tiles stacked across the batch dimension, using tile blend masks. * Variables: * tiles (torch.Tensor): A set of tiles that may or not be masked, stacked across the batch dimension. * border (int, optional): Optionally add a border of a specified size to the edges of tiles in the full image for debugging and explainability. Set to None for no border. * colors (list of float, optional): A set of floats to use for the border color, if using borders. Default is set to red unless specified. * Returns: * full_image (torch.Tensor): The full image made up of tiles blended together using masks.

forward(x): Takes a stack of tiles, combines them into the full image with blending masks, then splits the image back into tiles. * Variables: * x (torch.Tensor): A set of tiles to blend the overlapping regions together of. * Returns: * x (torch.Tensor): A set of tiles with overlapping regions blended together.

Supported Tensor Types

The TilingModule class has been tested with and is confirmed to work with the following PyTorch Tensor Data types / dtypes: torch.float32 / torch.float, torch.float64 / torch.double, torch.float16 / torch.half, & torch.bfloat16.

Usage

The TilingModule class is pretty easy to use.

``` from blended_tiling import TilingModule

fullsize = [512, 512] tilesize = [224, 224] tile_overlap = [0.25, 0.25] # 25% overlap on both H & W

tilingmodule = TilingModule( tilesize=tilesize, tileoverlap=tileoverlap, basesize=full_size, )

Shape of tiles expected in forward pass

inputshape = [tilingmodule.numtiles(), 3] + tilesize

Tiles are blended together and then split apart by default

blendedtiles = tilingmodule(torch.ones(input_shape)) ```

Tiles can be created and then merged back into the original tensor like this:

``` full_tensor = torch.ones(1, 3, 512, 512)

tiles = tilingmodule.splitintotiles(fulltensor)

fulltensor = tilingmodule.rebuildwithmasks(tiles) ```

The tile boundaries can be viewed on the full tensor like this:

tiles = torch.ones(9, 3, 224, 224) full_tensor = tiling_module.rebuild_with_masks(tiles, border=2)

And the number of tiles and tiling pattern can be obtained like this:

``` numtiles = tilingmodule.num_tiles()

tilingpattern = tilingmodule.tilingpattern() print("{}x{}".format(tilingpattern[0], tiling_pattern[1])) ```

Custom Classes

It's also easy to modify the forward function of the tiling module:

``` from typing import Union, List, Tuple from blended_tiling import TilingModule

class CustomTilingModule(TilingModule): def init( self, tilesize: Union[int, List[int], Tuple[int, int]] = [224, 224], tileoverlap: Union[float, List[float], Tuple[float, float]] = [0.25, 0.25], basesize: Union[int, List[int], Tuple[int, int]] = [512, 512], ) -> None: TilingModule.init(self, tilesize, tileoverlap, basesize) self.custom_module = torch.nn.Identity()

def forward(self, x: torch.Tensor) -> torch.Tensor:
    x = self.rebuild_with_masks(x)
    x = self.custom_module(x) + 4.0
    return self._get_tiles_and_coords(x)[0]

```

Examples

To demonstrate the tile blending abilities of the TilingModule class, an example has been created below.

First we'll create a set of tiles & give them all unique colors for this example:

```

Setup TilingModule instance

fullsize = [768, 1014] tilesize = [256, 448] tileoverlap = [0.25, 0.25] tilingmodule = TilingModule( tilesize=tilesize, tileoverlap=tileoverlap, basesize=fullsize, )

Create unique colors for tiles

tilecolors = [ [0.5334, 0.0, 0.8459], [0.0, 1.0, 0.0], [0.0, 0.7071, 0.7071], [0.7071, 0.7071, 0.0], [1.0, 0.0, 0.0], [0.8459, 0.0, 0.5334], [0.7071, 0.0, 0.7071], [0.0, 0.8459, 0.5334], [0.5334, 0.8459, 0.0], [0.0, 0.5334, 0.8459], [0.0, 0.0, 1.0], [0.8459, 0.5334, 0.0], ] tilecolors = torch.astensor(tilecolors).view(12, 3, 1, 1)

Create tiles

tiles = torch.ones([tilingmodule.numtiles(), 3] + tile_size)

Color tiles

tiles = tiles * tile_colors ```

Next we apply the blend masks to the tiles:

tiles = tiles * tiling_module.get_tile_masks()

We can now combine the masked tiles into the full image:

```

Build full tiled image

output = tiling_module.rebuild(tiles) ```

We can also view the tile boundaries like so:

```

Build full tiled image

output = tiling_module.rebuild(tiles, border=2, colors=[0,0,0]) ```

We can view an animation of the tiles being added like this:

``` from torchvision.transforms import ToPILImage

tilesteps = [ tilingmodule.rebuild(tiles[: i + 1]) for i in range(tiles.shape[0]) ] tileframes = [ ToPILImage()(x[0]) for x in [torch.zeroslike(tilesteps[0])] + tilesteps + [tilesteps[-1]] ] tileframes[0].save( "tiles.gif", format="GIF", appendimages=tileframes[1:], save_all=True, duration=700, loop=0, ) ```

Owner

  • Login: ProGamerGov
  • Kind: user
  • Location: Multiverse

Citation (CITATION.cff)

abstract: "Blended tiling for activations & image rendering."
authors:
  - family-names: Egan
    given-names: Ben
cff-version: 1.2.0
date-released: "2022-06-22"
identifiers:
  - description: "This is the collection of archived snapshots of all versions of blended-tiling"
    type: doi
    value: 10.5281/zenodo.6647960
keywords:
  - "blended tiling"
  - research
license: MIT
message: "If you use this software, please cite it using these metadata."
repository-code: "https://github.com/ProGamerGov/blended-tiling"
title: "Blended-Tiling"
version: 0.0.1-dev.7

GitHub Events

Total
  • Watch event: 1
  • Delete event: 1
  • Push event: 3
  • Pull request event: 2
  • Create event: 1
Last Year
  • Watch event: 1
  • Delete event: 1
  • Push event: 3
  • Pull request event: 2
  • Create event: 1

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 14
  • Total Committers: 2
  • Avg Commits per committer: 7.0
  • Development Distribution Score (DDS): 0.071
Past Year
  • Commits: 1
  • Committers: 1
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
ProGamerGov P****v 13
antoine-scenario 1****o 1

Issues and Pull Requests

Last synced: 8 months ago

All Time
  • Total issues: 0
  • Total pull requests: 11
  • Average time to close issues: N/A
  • Average time to close pull requests: about 5 hours
  • Total issue authors: 0
  • Total pull request authors: 2
  • Average comments per issue: 0
  • Average comments per pull request: 0.09
  • Merged pull requests: 10
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: less than a minute
  • Issue authors: 0
  • Pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
  • ProGamerGov (11)
  • antoine-scenario (1)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 7,757 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 1
  • Total versions: 7
  • Total maintainers: 2
pypi.org: blended-tiling

Blended tiling with PyTorch

  • Versions: 7
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 7,757 Last month
Rankings
Dependent packages count: 10.1%
Downloads: 12.1%
Stargazers count: 13.2%
Average: 15.2%
Forks count: 19.1%
Dependent repos count: 21.5%
Maintainers (2)
Last synced: 7 months ago

Dependencies

setup.py pypi
  • torch >=1.6