Hsyshasuzh's picture
download
raw
1.56 kB
"""Native-resolution tiles and central-context categorical stitching."""
from dataclasses import dataclass
import numpy as np
@dataclass
class Tile:
image: np.ndarray
x: int
y: int
width: int
height: int
def origins(length, size, overlap):
if length <= size:
return [0]
stride = max(1, round(size * (1 - overlap)))
return sorted(set([*range(0, length - size + 1, stride), length - size]))
def iter_tiles(image, tile_size=1024, overlap=.25):
if type(tile_size) is not int or tile_size != 1024 or not .2 <= overlap <= .25:
raise ValueError('native 1024 tiles and overlap from .20 to .25 required')
height, width = image.shape[:2]
for y in origins(height, tile_size, overlap):
for x in origins(width, tile_size, overlap):
patch = image[y:y + tile_size, x:x + tile_size]
h, w = patch.shape[:2]
padded = np.zeros((tile_size, tile_size, 3), np.uint8)
padded[:h, :w] = patch
yield Tile(padded, x, y, w, h)
def stitch(mask, weights, tile, prediction, valid):
"""Choose the tile with the most context; never average categorical IDs."""
h, w = tile.height, tile.width
yy, xx = np.ogrid[:h, :w]
centrality = np.minimum(np.minimum(yy + 1, h - yy), np.minimum(xx + 1, w - xx)).astype(np.uint16)
region = np.s_[tile.y:tile.y + h, tile.x:tile.x + w]
current = weights[region]
take = valid[:h, :w] & (centrality > current)
mask[region][take] = prediction[:h, :w][take]
current[take] = centrality[take]

Xet Storage Details

Size:
1.56 kB
·
Xet hash:
172270dc9edc04384702cade44cd691b657e1e77c000971a154aa9f4bfba7176

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.