def blur(field, radius, cutoff=None, kernel="1/1+x"): """ Warning: This function can cause NaN in the gradients, reason unknown. Runs a blur kernel over the given tensor. :param field: tensor :param radius: weight function curve scale :param cutoff: kernel size :param kernel: Type of blur kernel (str). Must be in ('1/1+x', 'gauss') :return: """ if cutoff is None: cutoff = min(int(round(radius * 3)), *field.shape[1:-1]) xyz = np.meshgrid( *[range(-int(cutoff), (cutoff) + 1) for _ in field.shape[1:-1]]) d = math.to_float(np.sqrt(np.sum([x**2 for x in xyz], axis=0))) if kernel == "1/1+x": weights = math.to_float(1) / (d / radius + 1) elif kernel.lower() == "gauss": weights = math.exp(-d / radius / 2) else: raise ValueError("Unknown kernel: %s" % kernel) weights /= math.sum(weights) weights = math.reshape(weights, list(weights.shape) + [1, 1]) return math.conv(field, weights)
def upsample2x(tensor, interpolation='linear'): if struct.isstruct(tensor): return struct.map(lambda s: upsample2x(s, interpolation), tensor, recursive=False) if interpolation.lower() != 'linear': raise ValueError('Only linear interpolation supported') dims = range(spatial_rank(tensor)) vlen = tensor.shape[-1] spatial_dims = tensor.shape[1:-1] rank = spatial_rank(tensor) tensor = math.pad(tensor, _get_pad_width(rank), 'replicate') for dim in dims: lower, center, upper = _dim_shifted(tensor, dim, (-1, 0, 1)) combined = math.stack([0.25 * lower + 0.75 * center, 0.75 * center + 0.25 * upper], axis=2 + dim) tensor = math.reshape(combined, [-1] + [spatial_dims[dim] * 2 if i == dim else tensor.shape[i + 1] for i in dims] + [vlen]) return tensor