(data_shape, target_shape, image_size=None,
        scale_offset=None, dtype=torch.float, device=None)
return grid
Prepares a grid to use with grid_sample to upsample a batch of
    features in data_shape to the target_shape. Can use scale_offset
    and image_size to center the grid in a nondefault way: scale_offset
    maps feature pixels to image_size pixels, and it is assumed that
    the target_shape is a uniform downsampling of image_size.
Prepares a grid to use with grid_sample to upsample a batch of
    features in data_shape to the target_shape. Can use scale_offset
    and image_size to center the grid in a nondefault way: scale_offset
    maps feature pixels to image_size pixels, and it is assumed that
    the target_shape is a uniform downsampling of image_size.
Prepares a grid to use with grid_sample to upsample a batch of features in data_shape to the target_shape . Can use scale_offset and image_size to center the grid in a nondefault way : scale_offset maps feature pixels to image_size pixels and it is assumed that the target_shape is a uniform downsampling of image_size .
def upsample_grid(data_shape, target_shape, image_size=None,
        scale_offset=None, dtype=torch.float, device=None):
    '''Prepares a grid to use with grid_sample to upsample a batch of
    features in data_shape to the target_shape. Can use scale_offset
    and image_size to center the grid in a nondefault way: scale_offset
    maps feature pixels to image_size pixels, and it is assumed that
    the target_shape is a uniform downsampling of image_size.'''
    # Default is that nothing is resized.
    if target_shape is None:
        target_shape = data_shape
    # Make a default scale_offset to fill the image if there isn't one
    if scale_offset is None:
        scale = tuple(float(ts) / ds
                for ts, ds in zip(target_shape, data_shape))
        offset = tuple(0.5 * s - 0.5 for s in scale)
    else:
        scale, offset = (v for v in zip(*scale_offset))
        # Handle downsampling for different input vs target shape.
        if image_size is not None:
            scale = tuple(s * (ts - 1) / (ns - 1)
                    for s, ns, ts in zip(scale, image_size, target_shape))
            offset = tuple(o * (ts - 1) / (ns - 1)
                    for o, ns, ts in zip(offset, image_size, target_shape))
    # Pytorch needs target coordinates in terms of source coordinates [-1..1]
    ty, tx = (((torch.arange(ts, dtype=dtype, device=device) - o)
                  * (2 / (s * (ss - 1))) - 1)
        for ts, ss, s, o, in zip(target_shape, data_shape, scale, offset))
    # Whoa, note that grid_sample reverses the order y, x -> x, y.
    grid = torch.stack(
        (tx[None,:].expand(target_shape), ty[:,None].expand(target_shape)),2
       )[None,:,:,:].expand((1, target_shape[0], target_shape[1], 2))
    return grid
def upsample_grid ( data_shape , target_shape , image_size = None , scale_offset = None , dtype = torch . float , device = None ) : # Default is that nothing is resized. if target_shape is None : target_shape = data_shape # Make a default scale_offset to fill the image if there isn't one if scale_offset is None : scale = tuple ( float ( ts ) / ds for ts , ds in zip ( target_shape , data_shape ) ) offset = tuple ( 0.5 * s - 0.5 for s in scale ) else : scale , offset = ( v for v in zip ( * scale_offset ) ) # Handle downsampling for different input vs target shape. if image_size is not None : scale = tuple ( s * ( ts - 1 ) / ( ns - 1 ) for s , ns , ts in zip ( scale , image_size , target_shape ) ) offset = tuple ( o * ( ts - 1 ) / ( ns - 1 ) for o , ns , ts in zip ( offset , image_size , target_shape ) ) # Pytorch needs target coordinates in terms of source coordinates [-1..1] ty , tx = ( ( ( torch . arange ( ts , dtype = dtype , device = device ) - o ) * ( 2 / ( s * ( ss - 1 ) ) ) - 1 ) for ts , ss , s , o , in zip ( target_shape , data_shape , scale , offset ) ) # Whoa, note that grid_sample reverses the order y, x -> x, y. grid = torch . stack ( ( tx [ None , : ] . expand ( target_shape ) , ty [ : , None ] . expand ( target_shape ) ) , 2 ) [ None , : , : , : ] . expand ( ( 1 , target_shape [ 0 ] , target_shape [ 1 ] , 2 ) ) return grid