MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / upsample_2d

Function upsample_2d

diffusers/src/diffusers/models/upsampling.py:465–509  ·  view source on GitHub ↗

r"""Upsample2D a batch of 2D images with the given filter. Accepts a batch of 2D images of the shape `[N, C, H, W]` or `[N, H, W, C]` and upsamples each image with the given filter. The filter is normalized so that if the input pixels are constant, they will be scaled by the specified `g

(
    hidden_states: torch.Tensor,
    kernel: Optional[torch.Tensor] = None,
    factor: int = 2,
    gain: float = 1,
)

Source from the content-addressed store, hash-verified

463
464
465def upsample_2d(
466 hidden_states: torch.Tensor,
467 kernel: Optional[torch.Tensor] = None,
468 factor: int = 2,
469 gain: float = 1,
470) -> torch.Tensor:
471 r"""Upsample2D a batch of 2D images with the given filter.
472 Accepts a batch of 2D images of the shape `[N, C, H, W]` or `[N, H, W, C]` and upsamples each image with the given
473 filter. The filter is normalized so that if the input pixels are constant, they will be scaled by the specified
474 `gain`. Pixels outside the image are assumed to be zero, and the filter is padded with zeros so that its shape is
475 a: multiple of the upsampling factor.
476
477 Args:
478 hidden_states (`torch.Tensor`):
479 Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
480 kernel (`torch.Tensor`, *optional*):
481 FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
482 corresponds to nearest-neighbor upsampling.
483 factor (`int`, *optional*, default to `2`):
484 Integer upsampling factor.
485 gain (`float`, *optional*, default to `1.0`):
486 Scaling factor for signal magnitude (default: 1.0).
487
488 Returns:
489 output (`torch.Tensor`):
490 Tensor of the shape `[N, C, H * factor, W * factor]`
491 """
492 assert isinstance(factor, int) and factor >= 1
493 if kernel is None:
494 kernel = [1] * factor
495
496 kernel = torch.tensor(kernel, dtype=torch.float32)
497 if kernel.ndim == 1:
498 kernel = torch.outer(kernel, kernel)
499 kernel /= torch.sum(kernel)
500
501 kernel = kernel * (gain * (factor**2))
502 pad_value = kernel.shape[0] - factor
503 output = upfirdn2d_native(
504 hidden_states,
505 kernel.to(device=hidden_states.device),
506 up=factor,
507 pad=((pad_value + 1) // 2 + factor - 1, pad_value // 2),
508 )
509 return output

Callers 1

__init__Method · 0.85

Calls 2

upfirdn2d_nativeFunction · 0.85
toMethod · 0.45

Tested by

no test coverage detected