MCPcopy Create free account
hub / github.com/Meshcapade/difflocks / resize

Function resize

utils/resize_right/resize_right.py:32–123  ·  view source on GitHub ↗
(input, scale_factors=None, out_shape=None,
           interp_method=interp_methods.cubic, support_sz=None,
           antialiasing=True, by_convs=False, scale_tolerance=None,
           max_numerator=10, pad_mode='constant')

Source from the content-addressed store, hash-verified

30
31
32def resize(input, scale_factors=None, out_shape=None,
33 interp_method=interp_methods.cubic, support_sz=None,
34 antialiasing=True, by_convs=False, scale_tolerance=None,
35 max_numerator=10, pad_mode='constant'):
36 # get properties of the input tensor
37 in_shape, n_dims = input.shape, input.ndim
38
39 # fw stands for framework that can be either numpy or torch,
40 # determined by the input type
41 fw = numpy if type(input) is numpy.ndarray else torch
42 eps = fw.finfo(fw.float32).eps
43 device = input.device if fw is torch else None
44
45 # set missing scale factors or output shapem one according to another,
46 # scream if both missing. this is also where all the defults policies
47 # take place. also handling the by_convs attribute carefully.
48 scale_factors, out_shape, by_convs = set_scale_and_out_sz(in_shape,
49 out_shape,
50 scale_factors,
51 by_convs,
52 scale_tolerance,
53 max_numerator,
54 eps, fw)
55
56 # sort indices of dimensions according to scale of each dimension.
57 # since we are going dim by dim this is efficient
58 sorted_filtered_dims_and_scales = [(dim, scale_factors[dim], by_convs[dim],
59 in_shape[dim], out_shape[dim])
60 for dim in sorted(range(n_dims),
61 key=lambda ind: scale_factors[ind])
62 if scale_factors[dim] != 1.]
63
64 # unless support size is specified by the user, it is an attribute
65 # of the interpolation method
66 if support_sz is None:
67 support_sz = interp_method.support_sz
68
69 # output begins identical to input and changes with each iteration
70 output = input
71
72 # iterate over dims
73 for (dim, scale_factor, dim_by_convs, in_sz, out_sz
74 ) in sorted_filtered_dims_and_scales:
75 # STEP 1- PROJECTED GRID: The non-integer locations of the projection
76 # of output pixel locations to the input tensor
77 projected_grid = get_projected_grid(in_sz, out_sz,
78 scale_factor, fw, dim_by_convs,
79 device)
80
81 # STEP 1.5: ANTIALIASING- If antialiasing is taking place, we modify
82 # the window size and the interpolation method (see inside function)
83 cur_interp_method, cur_support_sz = apply_antialiasing_if_needed(
84 interp_method,
85 support_sz,
86 scale_factor,
87 antialiasing)
88
89 # STEP 2- FIELDS OF VIEW: for each output pixels, map the input pixels

Callers

nothing calls this directly

Calls 8

set_scale_and_out_szFunction · 0.85
get_projected_gridFunction · 0.85
get_field_of_viewFunction · 0.85
calc_pad_szFunction · 0.85
get_weightsFunction · 0.85
apply_weightsFunction · 0.85
apply_convsFunction · 0.85

Tested by

no test coverage detected