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

Function imresize_np

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

Source from the content-addressed store, hash-verified

837# imresize for numpy image [0, 1]
838# --------------------------------------------
839def imresize_np(img, scale, antialiasing=True):
840 # Now the scale should be the same for H and W
841 # input: img: Numpy, HWC or HW [0,1]
842 # output: HWC or HW [0,1] w/o round
843 img = torch.from_numpy(img)
844 need_squeeze = True if img.dim() == 2 else False
845 if need_squeeze:
846 img.unsqueeze_(2)
847
848 in_H, in_W, in_C = img.size()
849 out_C, out_H, out_W = in_C, math.ceil(in_H * scale), math.ceil(in_W * scale)
850 kernel_width = 4
851 kernel = 'cubic'
852
853 # Return the desired dimension order for performing the resize. The
854 # strategy is to perform the resize first along the dimension with the
855 # smallest scale factor.
856 # Now we do not support this.
857
858 # get weights and indices
859 weights_H, indices_H, sym_len_Hs, sym_len_He = calculate_weights_indices(
860 in_H, out_H, scale, kernel, kernel_width, antialiasing)
861 weights_W, indices_W, sym_len_Ws, sym_len_We = calculate_weights_indices(
862 in_W, out_W, scale, kernel, kernel_width, antialiasing)
863 # process H dimension
864 # symmetric copying
865 img_aug = torch.FloatTensor(in_H + sym_len_Hs + sym_len_He, in_W, in_C)
866 img_aug.narrow(0, sym_len_Hs, in_H).copy_(img)
867
868 sym_patch = img[:sym_len_Hs, :, :]
869 inv_idx = torch.arange(sym_patch.size(0) - 1, -1, -1).long()
870 sym_patch_inv = sym_patch.index_select(0, inv_idx)
871 img_aug.narrow(0, 0, sym_len_Hs).copy_(sym_patch_inv)
872
873 sym_patch = img[-sym_len_He:, :, :]
874 inv_idx = torch.arange(sym_patch.size(0) - 1, -1, -1).long()
875 sym_patch_inv = sym_patch.index_select(0, inv_idx)
876 img_aug.narrow(0, sym_len_Hs + in_H, sym_len_He).copy_(sym_patch_inv)
877
878 out_1 = torch.FloatTensor(out_H, in_W, in_C)
879 kernel_width = weights_H.size(1)
880 for i in range(out_H):
881 idx = int(indices_H[i][0])
882 for j in range(out_C):
883 out_1[i, :, j] = img_aug[idx:idx + kernel_width, :, j].transpose(0, 1).mv(weights_H[i])
884
885 # process W dimension
886 # symmetric copying
887 out_1_aug = torch.FloatTensor(out_H, in_W + sym_len_Ws + sym_len_We, in_C)
888 out_1_aug.narrow(1, sym_len_Ws, in_W).copy_(out_1)
889
890 sym_patch = out_1[:, :sym_len_Ws, :]
891 inv_idx = torch.arange(sym_patch.size(1) - 1, -1, -1).long()
892 sym_patch_inv = sym_patch.index_select(1, inv_idx)
893 out_1_aug.narrow(1, 0, sym_len_Ws).copy_(sym_patch_inv)
894
895 sym_patch = out_1[:, -sym_len_We:, :]
896 inv_idx = torch.arange(sym_patch.size(1) - 1, -1, -1).long()

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected