torchsample.coord module
torchsample.coord module#
- torchsample.coord.full(batch, size, device=None, align_corners=False)[source]#
Generate 2D or 3D coordinates to fully n_samples an image.
- Parameters
batch (int) – Batch size. If
0
, don’t return a batch dimension.size (tuple) – Size of field to generate pixel coordinates for. i.e.
(x, y, ...)
.device (torch.device) – The desired device of returned tensor
align_corners (bool) – if
True
, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels.
- Returns
coords –
(n, h, w, 2)
Normalized coordinates for sampling functions.- Return type
torch.Tensor
- torchsample.coord.normalize(coord, size, align_corners=False)[source]#
Normalize unnormalized coordinates.
unnormalize
- Parameters
coord (torch.Tensor) – Values in range
[0, size-1]
to normalize.size (int or tuple) – Unnormalized side length in pixels. If tuple, order is
(x, y, ...)
to matchcoord
.align_corners (bool) – if
True
, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels.
- Returns
Normalized coordinates.
- Return type
torch.Tensor
- torchsample.coord.rand(batch, n_samples, dims=2, dtype=None, device=None)[source]#
Generate random coordinates in range
[-1, 1]
.- Parameters
batch (int) – Batch size. If
0
, don’t return a batch dimension.n_samples (int) – Number of coordinates to generate per exemplar.
dims (int) –
2
for generating(x, y)
coordinates.3
for generating(x, y, z)
coordinates. Defaults to2
.dtype (torch.dtype) – The desired data type of returned tensor.
device (torch.device) – The desired device of returned tensor
- Return type
(batch, n_samples, dims) random coordinates in range
[-1, 1]
.
- torchsample.coord.rand_biased(batch, n_samples, pred, k=3.0, beta=0.75, align_corners=False)[source]#
Uncertainty-based point sampling.
- Described in training part of section 3.1 of:
PointRend: Image Segmentation as Rendering
- Parameters
batch (int) – Batch size. If
0
, don’t return a batch dimension.n_samples (int) – Number of coordinates to generate per exemplar.
pred (torch.Tensor) –
(b, c, h, w)
or(b, c, d, h, w)
prediction probabilities.k (float) – Multiplier for oversampling for biased coordinates.
beta (float) – Portion of coordinates to be biased towards uncertain regions. Lower values of beta are more similar to a uniform random distribution, ignoring
pred
.align_corners (bool) – if
True
, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels.
- Returns
(b, n_samples, dim) Normalized coordinates.
- Return type
torch.Tensor
- torchsample.coord.randint(batch, n_samples, size, device=None, align_corners=False, replace=False)[source]#
Generate random pixel coordinates in range
[-1, 1]
.Unlike
rand
, the resulting coordinates will land exactly on pixels. Intended for sampling high resolution GT data during data loading.Due to numerical precision, it is suggested to use
mode="nearest"
when sampling featuremaps of resolutionsize
using these coordinates.- Parameters
batch (int) – Batch size. If
0
, don’t return a batch dimension.n_samples (int) –
size (tuple) – Size of field to generate pixel coordinates for. i.e.
(x, y, ...)
.device (torch.device) – The desired device of returned tensor
align_corners (bool) – if
True
, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels.replace (bool) – Sample with or without replacement. Defaults to
False
.
- Return type
(batch, n_samples, dims) random coordinates in range
[-1, 1]
.
- torchsample.coord.randint_like(n_samples, tensor, align_corners=False, replace=False)[source]#
Generate random pixel coordinates in range
[-1, 1]
.See
randint
.- Parameters
n_samples (int) –
tensor (tuple) – Tensor to generate random coordinates for. Coordinates will be placed on same device as
tensor
.align_corners (bool) – if
True
, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels.replace (bool) – Sample with or without replacement. Defaults to
True
.
- Return type
(batch, n_samples, dims) random coordinates in range
[-1, 1]
.
- torchsample.coord.uncertain(pred, threshold=0.5, align_corners=False)[source]#
Report all coordinates with uncertain scores.
Used for inference with models trained with
rand_biased
.features = feature_extractor(batch["image"]) ss_pred = decoder(batch["image"]) index_coords, norm_coords = ts.coord.uncertain(ss_pred) features_sampled = ts.sample(norm_coords, features) refined = mlp(features_sampled) ss_pred_refined = ss_pred.clone() ss_pred_refined[index_coords] = ts.feat_first(refined).flatten()
- Parameters
pred (torch.Tensor) –
(1, c, h, w)
or(1, c, d, h, w)
prediction probabilities. Can NOT handle>1
batch size; would result in ragged tensors.threshold (float) – Coords with a max probability lower than this threshold will have coordinates generated.
align_corners (bool) – if
True
, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels.
- Returns
index_coords (tuple) – Integer pixel coordinates that can be used to directly index into
pred
.norm_coords – Normalized coordinates to be used with
ts.sample
. Does NOT have a batch dimension, as it would end up with a ragged tensor.
- torchsample.coord.unnormalize(coord, size, align_corners=False, clip=False)[source]#
Unnormalize normalized coordinates.
- Modified from PyTorch C source:
- Parameters
coord (torch.Tensor) – Values in range
[-1, 1]
to unnormalize.size (int or tuple) – Unnormalized side length in pixels. If tuple, order is
(x, y, ...)
to matchcoord
.align_corners (bool) – if
True
, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels.clip (bool) – If
True
, output will be clipped to range[0, size-1]
- Returns
Unnormalized coordinates.
- Return type
torch.Tensor