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

Function imresize

ldm/modules/image_degradation/utils_image.py:766–833  ·  view source on GitHub ↗
(img, scale, antialiasing=True)

Source from the content-addressed store, hash-verified

764# imresize for tensor image [0, 1]
765# --------------------------------------------
766def imresize(img, scale, antialiasing=True):
767 # Now the scale should be the same for H and W
768 # input: img: pytorch tensor, CHW or HW [0,1]
769 # output: CHW or HW [0,1] w/o round
770 need_squeeze = True if img.dim() == 2 else False
771 if need_squeeze:
772 img.unsqueeze_(0)
773 in_C, in_H, in_W = img.size()
774 out_C, out_H, out_W = in_C, math.ceil(in_H * scale), math.ceil(in_W * scale)
775 kernel_width = 4
776 kernel = 'cubic'
777
778 # Return the desired dimension order for performing the resize. The
779 # strategy is to perform the resize first along the dimension with the
780 # smallest scale factor.
781 # Now we do not support this.
782
783 # get weights and indices
784 weights_H, indices_H, sym_len_Hs, sym_len_He = calculate_weights_indices(
785 in_H, out_H, scale, kernel, kernel_width, antialiasing)
786 weights_W, indices_W, sym_len_Ws, sym_len_We = calculate_weights_indices(
787 in_W, out_W, scale, kernel, kernel_width, antialiasing)
788 # process H dimension
789 # symmetric copying
790 img_aug = torch.FloatTensor(in_C, in_H + sym_len_Hs + sym_len_He, in_W)
791 img_aug.narrow(1, sym_len_Hs, in_H).copy_(img)
792
793 sym_patch = img[:, :sym_len_Hs, :]
794 inv_idx = torch.arange(sym_patch.size(1) - 1, -1, -1).long()
795 sym_patch_inv = sym_patch.index_select(1, inv_idx)
796 img_aug.narrow(1, 0, sym_len_Hs).copy_(sym_patch_inv)
797
798 sym_patch = img[:, -sym_len_He:, :]
799 inv_idx = torch.arange(sym_patch.size(1) - 1, -1, -1).long()
800 sym_patch_inv = sym_patch.index_select(1, inv_idx)
801 img_aug.narrow(1, sym_len_Hs + in_H, sym_len_He).copy_(sym_patch_inv)
802
803 out_1 = torch.FloatTensor(in_C, out_H, in_W)
804 kernel_width = weights_H.size(1)
805 for i in range(out_H):
806 idx = int(indices_H[i][0])
807 for j in range(out_C):
808 out_1[j, i, :] = img_aug[j, idx:idx + kernel_width, :].transpose(0, 1).mv(weights_H[i])
809
810 # process W dimension
811 # symmetric copying
812 out_1_aug = torch.FloatTensor(in_C, out_H, in_W + sym_len_Ws + sym_len_We)
813 out_1_aug.narrow(2, sym_len_Ws, in_W).copy_(out_1)
814
815 sym_patch = out_1[:, :, :sym_len_Ws]
816 inv_idx = torch.arange(sym_patch.size(2) - 1, -1, -1).long()
817 sym_patch_inv = sym_patch.index_select(2, inv_idx)
818 out_1_aug.narrow(2, 0, sym_len_Ws).copy_(sym_patch_inv)
819
820 sym_patch = out_1[:, :, -sym_len_We:]
821 inv_idx = torch.arange(sym_patch.size(2) - 1, -1, -1).long()
822 sym_patch_inv = sym_patch.index_select(2, inv_idx)
823 out_1_aug.narrow(2, sym_len_Ws + in_W, sym_len_We).copy_(sym_patch_inv)

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected