MCPcopy Create free account
hub / github.com/VisionXLab/OF-Diff / get_fold_unfold

Method get_fold_unfold

ldm/models/diffusion/ddpm.py:722–772  ·  view source on GitHub ↗

:param x: img of size (bs, c, h, w) :return: n img crops of size (n, bs, c, kernel_size[0], kernel_size[1])

(self, x, kernel_size, stride, uf=1, df=1)

Source from the content-addressed store, hash-verified

720 return weighting
721
722 def get_fold_unfold(self, x, kernel_size, stride, uf=1, df=1): # todo load once not every time, shorten code
723 """
724 :param x: img of size (bs, c, h, w)
725 :return: n img crops of size (n, bs, c, kernel_size[0], kernel_size[1])
726 """
727 bs, nc, h, w = x.shape
728
729 # number of crops in image
730 Ly = (h - kernel_size[0]) // stride[0] + 1
731 Lx = (w - kernel_size[1]) // stride[1] + 1
732
733 if uf == 1 and df == 1:
734 fold_params = dict(kernel_size=kernel_size, dilation=1, padding=0, stride=stride)
735 unfold = torch.nn.Unfold(**fold_params)
736
737 fold = torch.nn.Fold(output_size=x.shape[2:], **fold_params)
738
739 weighting = self.get_weighting(kernel_size[0], kernel_size[1], Ly, Lx, x.device).to(x.dtype)
740 normalization = fold(weighting).view(1, 1, h, w) # normalizes the overlap
741 weighting = weighting.view((1, 1, kernel_size[0], kernel_size[1], Ly * Lx))
742
743 elif uf > 1 and df == 1:
744 fold_params = dict(kernel_size=kernel_size, dilation=1, padding=0, stride=stride)
745 unfold = torch.nn.Unfold(**fold_params)
746
747 fold_params2 = dict(kernel_size=(kernel_size[0] * uf, kernel_size[0] * uf),
748 dilation=1, padding=0,
749 stride=(stride[0] * uf, stride[1] * uf))
750 fold = torch.nn.Fold(output_size=(x.shape[2] * uf, x.shape[3] * uf), **fold_params2)
751
752 weighting = self.get_weighting(kernel_size[0] * uf, kernel_size[1] * uf, Ly, Lx, x.device).to(x.dtype)
753 normalization = fold(weighting).view(1, 1, h * uf, w * uf) # normalizes the overlap
754 weighting = weighting.view((1, 1, kernel_size[0] * uf, kernel_size[1] * uf, Ly * Lx))
755
756 elif df > 1 and uf == 1:
757 fold_params = dict(kernel_size=kernel_size, dilation=1, padding=0, stride=stride)
758 unfold = torch.nn.Unfold(**fold_params)
759
760 fold_params2 = dict(kernel_size=(kernel_size[0] // df, kernel_size[0] // df),
761 dilation=1, padding=0,
762 stride=(stride[0] // df, stride[1] // df))
763 fold = torch.nn.Fold(output_size=(x.shape[2] // df, x.shape[3] // df), **fold_params2)
764
765 weighting = self.get_weighting(kernel_size[0] // df, kernel_size[1] // df, Ly, Lx, x.device).to(x.dtype)
766 normalization = fold(weighting).view(1, 1, h // df, w // df) # normalizes the overlap
767 weighting = weighting.view((1, 1, kernel_size[0] // df, kernel_size[1] // df, Ly * Lx))
768
769 else:
770 raise NotImplementedError
771
772 return fold, unfold, normalization, weighting
773
774 @torch.no_grad()
775 def get_input(self, batch, k, return_first_stage_outputs=False, force_c_encode=False,

Callers

nothing calls this directly

Calls 1

get_weightingMethod · 0.95

Tested by

no test coverage detected