Пример #1
0
 def reduce(x: torch.Tensor, operation: str, reduced_axes: List[int]):
     if operation == 'min':
         return x.amin(dim=reduced_axes)
     elif operation == 'max':
         return x.amax(dim=reduced_axes)
     elif operation == 'sum':
         return x.sum(dim=reduced_axes)
     elif operation == 'mean':
         return x.mean(dim=reduced_axes)
     elif operation == 'prod':
         for i in list(sorted(reduced_axes))[::-1]:
             x = x.prod(dim=i)
         return x
     else:
         raise NotImplementedError('Unknown reduction ', operation)
Пример #2
0
def clamp_normalize(
    inputs: Tensor,
    minimum: float = float("-inf"),
    maximum: float = float("inf"),
    norm_min: float = 0.0,
    norm_max: float = 1.0,
    inplace: bool = True,
    eps: float = 1e-8,
) -> Tensor:
    if maximum <= minimum:
        raise ValueError(
            f"Expected maximum > minimum: got {maximum} vs {minimum}")
    if norm_max <= norm_min:
        raise ValueError(
            f"Expected norm_max > norm_min: got {norm_max} vs {norm_min}")
    if eps <= 0:
        raise ValueError(f"Expected eps > 0: got {eps}")

    if not inputs.is_floating_point():
        inputs = inputs.float()

    minimum = float(minimum) if minimum != float("-inf") else float(
        inputs.amin().item())
    maximum = float(maximum) if maximum != float("inf") else float(
        inputs.amax().item())

    delta = max(maximum - minimum, eps)
    output_delta = norm_max - norm_min
    assert output_delta > 0

    if inplace:
        outputs = inputs.clamp_(minimum, maximum).sub_(minimum).div_(delta)
    else:
        outputs = inputs.clamp(minimum, maximum).sub(minimum).div(delta)

    # only apply renormalization if necessary
    if norm_min != 0 or norm_max != 1:
        if inplace:
            outputs = outputs.mul_(output_delta).add_(norm_min)
        else:
            outputs = outputs.mul(output_delta).add(norm_min)

    return outputs
Пример #3
0
def autocontrast(img: Tensor) -> Tensor:

    _assert_image_tensor(img)

    if img.ndim < 3:
        raise TypeError(f"Input image tensor should have at least 3 dimensions, but found {img.ndim}")

    _assert_channels(img, [1, 3])

    bound = 1.0 if img.is_floating_point() else 255.0
    dtype = img.dtype if torch.is_floating_point(img) else torch.float32

    minimum = img.amin(dim=(-2, -1), keepdim=True).to(dtype)
    maximum = img.amax(dim=(-2, -1), keepdim=True).to(dtype)
    scale = bound / (maximum - minimum)
    eq_idxs = torch.isfinite(scale).logical_not()
    minimum[eq_idxs] = 0
    scale[eq_idxs] = 1

    return ((img - minimum) * scale).clamp(0, bound).to(img.dtype)
Пример #4
0
def autocontrast(img: Tensor) -> Tensor:

    _assert_image_tensor(img)

    if img.ndim < 3:
        raise TypeError("Input image tensor should have at least 3 dimensions, but found {}".format(img.ndim))

    _assert_channels(img, [1, 3])

    bound = 1.0 if img.is_floating_point() else 255.0
    dtype = img.dtype if torch.is_floating_point(img) else torch.float32

    minimum = img.amin(dim=(-2, -1), keepdim=True).to(dtype)
    maximum = img.amax(dim=(-2, -1), keepdim=True).to(dtype)
    eq_idxs = torch.where(minimum == maximum)[0]
    minimum[eq_idxs] = 0
    maximum[eq_idxs] = bound
    scale = bound / (maximum - minimum)

    return ((img - minimum) * scale).clamp(0, bound).to(img.dtype)