コード例 #1
0
ファイル: train.py プロジェクト: satopirka/nlp-nnabla
def distance(u, v, eps=1e-5):
    uu = F.sum(F.pow_scalar(u, 2), axis=1)
    vv = F.sum(F.pow_scalar(v, 2), axis=1)
    euclid_norm_pow2 = F.sum(F.pow_scalar(u - v, 2), axis=1)
    alpha = F.maximum2(F.constant(eps, shape=uu.shape), 1.0 - uu)
    beta = F.maximum2(F.constant(eps, shape=vv.shape), 1.0 - vv)

    return F.acosh(1 + 2 * euclid_norm_pow2 / (alpha * beta))
コード例 #2
0
    def forward_impl(self, inputs, outputs):
        x = inputs[0].data
        M = inputs[1].data
        y = outputs[0].data
        y.copy_from(x)

        if not self.training:
            return
        Mb = F.max(x, keepdims=True)
        F.maximum2(M, Mb, outputs=[M])
コード例 #3
0
def lab2rgb(input):
    input_trans = F.split(input, axis=1)
    L, a, b = F.split(input, axis=1)
    y = (L + 16.0) / 116.0
    x = (a / 500.0) + y
    z = y - (b / 200.0)
    neg_mask = F.less_scalar(z, 0).apply(need_grad=False)
    z = z * F.logical_not(neg_mask)
    mask_Y = F.greater_scalar(y, 0.2068966).apply(need_grad=False)
    mask_X = F.greater_scalar(x, 0.2068966).apply(need_grad=False)
    mask_Z = F.greater_scalar(z, 0.2068966).apply(need_grad=False)
    Y_1 = (y ** 3) * mask_Y
    Y_2 = L / (116. * 7.787) * F.logical_not(mask_Y)
    var_Y = Y_1 + Y_2

    X_1 = (x ** 3) * mask_X
    X_2 = (x - 16. / 116.) / 7.787 * F.logical_not(mask_X)
    var_X = X_1 + X_2

    Z_1 = (z ** 3) * mask_Z
    Z_2 = (z - 16. / 116.) / 7.787 * F.logical_not(mask_Z)
    var_Z = Z_1 + Z_2

    X = 0.95047 * var_X
    Y = 1.00000 * var_Y
    Z = 1.08883 * var_Z

    var_R = X * 3.2406 + Y * -1.5372 + Z * -0.4986
    var_G = X * -0.9689 + Y * 1.8758 + Z * 0.0415
    var_B = X * 0.0557 + Y * -0.2040 + Z * 1.0570

    mask_R = F.greater_scalar(var_R, 0.0031308).apply(need_grad=False)
    n_mask_R = F.logical_not(mask_R)
    R_1 = (1.055 * (F.maximum2(var_R, n_mask_R) ** (1 / 2.4)) - 0.055) * mask_R
    R_2 = (12.92 * var_R) * n_mask_R
    var_R = R_1 + R_2

    mask_G = F.greater_scalar(var_G, 0.0031308).apply(need_grad=False)
    n_mask_G = F.logical_not(mask_G)
    G_1 = (1.055 * (F.maximum2(var_G, n_mask_G) ** (1 / 2.4)) - 0.055) * mask_G
    G_2 = (12.92 * var_G) * n_mask_G
    var_G = G_1 + G_2

    mask_B = F.greater_scalar(var_B, 0.0031308).apply(need_grad=False)
    n_mask_B = F.logical_not(mask_B)
    B_1 = (1.055 * (F.maximum2(var_B, n_mask_B) ** (1 / 2.4)) - 0.055) * mask_B
    B_2 = (12.92 * var_B) * n_mask_B
    var_B = B_1 + B_2
    return F.stack(var_R, var_G, var_B, axis=1)
コード例 #4
0
ファイル: __init__.py プロジェクト: sony/nnabla-examples
    def chamfer_hausdorff_oneside_dists(X0, X1):
        b0 = X0.shape[0]
        b1 = X1.shape[0]

        sum_ = 0
        max_ = nn.NdArray.from_numpy_array(np.array(-np.inf))
        n = 0
        for i in tqdm.tqdm(range(0, b0, sub_batch_size),
                           desc="cdist-outer-loop"):
            x0 = nn.NdArray.from_numpy_array(X0[i:i + sub_batch_size])
            norm_x0 = F.sum(x0**2.0, axis=1, keepdims=True)
            min_ = nn.NdArray.from_numpy_array(np.ones(x0.shape[0]) * np.inf)
            for j in tqdm.tqdm(range(0, b1, sub_batch_size),
                               desc="cdist-inner-loop"):
                x1 = nn.NdArray.from_numpy_array(X1[j:j + sub_batch_size])
                # block pwd
                norm_x1 = F.transpose(F.sum(x1**2.0, axis=1, keepdims=True),
                                      (1, 0))
                x1_T = F.transpose(x1, (1, 0))
                x01 = F.affine(x0, x1_T)
                bpwd = (norm_x0 + norm_x1 - 2.0 * x01)**0.5
                # block min
                min_ = F.minimum2(min_, F.min(bpwd, axis=1))
            # sum/max over cols
            sum_ += F.sum(min_)
            n += bpwd.shape[0]
            max_ = F.maximum2(max_, F.max(min_))
        ocd = sum_.data / n
        ohd = max_.data
        return ocd, ohd
コード例 #5
0
def clip_quant_vals():
    p = nn.get_parameters()
    if cfg.w_quantize in [
            'parametric_fp_b_xmax', 'parametric_fp_d_xmax',
            'parametric_fp_d_b', 'parametric_pow2_b_xmax',
            'parametric_pow2_b_xmin', 'parametric_pow2_xmin_xmax'
    ]:
        for k in p:
            if 'Wquant' in k.split('/') or 'bquant' in k.split('/'):
                if k.endswith('/m'):  # range
                    p[k].data = clip_scalar(p[k].data,
                                            cfg.w_dynrange_min + 1e-5,
                                            cfg.w_dynrange_max - 1e-5)
                elif k.endswith('/n'):  # bits
                    p[k].data = clip_scalar(p[k].data,
                                            cfg.w_bitwidth_min + 1e-5,
                                            cfg.w_bitwidth_max - 1e-5)
                elif k.endswith('/d'):  # delta
                    if cfg.w_quantize == 'parametric_fp_d_xmax':
                        g = k.replace('/d', '/xmax')
                        min_value = F.minimum2(p[k].data, p[g].data - 1e-5)
                        max_value = F.maximum2(p[k].data + 1e-5, p[g].data)
                        p[k].data = min_value
                        p[g].data = max_value
                    p[k].data = clip_scalar(p[k].data,
                                            cfg.w_stepsize_min + 1e-5,
                                            cfg.w_stepsize_max - 1e-5)
                elif k.endswith('/xmin'):  # xmin
                    if cfg.w_quantize == 'parametric_pow2_xmin_xmax':
                        g = k.replace('/xmin', '/xmax')
                        min_value = F.minimum2(p[k].data, p[g].data - 1e-5)
                        max_value = F.maximum2(p[k].data + 1e-5, p[g].data)
                        p[k].data = min_value
                        p[g].data = max_value
                    p[k].data = clip_scalar(p[k].data, cfg.w_xmin_min + 1e-5,
                                            cfg.w_xmin_max - 1e-5)
                elif k.endswith('/xmax'):  # xmax
                    p[k].data = clip_scalar(p[k].data, cfg.w_xmax_min + 1e-5,
                                            cfg.w_xmax_max - 1e-5)

    if cfg.a_quantize in [
            'parametric_fp_b_xmax_relu', 'parametric_fp_d_xmax_relu',
            'parametric_fp_d_b_relu', 'parametric_pow2_b_xmax_relu',
            'parametric_pow2_b_xmin_relu', 'parametric_pow2_xmin_xmax_relu'
    ]:
        for k in p:
            if 'Aquant' in k.split('/'):
                if k.endswith('/m'):  # range
                    p[k].data = clip_scalar(p[k].data,
                                            cfg.a_dynrange_min + 1e-5,
                                            cfg.a_dynrange_max - 1e-5)
                elif k.endswith('/n'):  # bits
                    p[k].data = clip_scalar(p[k].data,
                                            cfg.a_bitwidth_min + 1e-5,
                                            cfg.a_bitwidth_max - 1e-5)
                elif k.endswith('/d'):  # delta
                    if cfg.a_quantize == 'parametric_fp_d_xmax_relu':
                        g = k.replace('/d', '/xmax')
                        min_value = F.minimum2(p[k].data, p[g].data - 1e-5)
                        max_value = F.maximum2(p[k].data + 1e-5, p[g].data)
                        p[k].data = min_value
                        p[g].data = max_value
                    p[k].data = clip_scalar(p[k].data,
                                            cfg.a_stepsize_min + 1e-5,
                                            cfg.a_stepsize_max - 1e-5)
                elif k.endswith('/xmin'):  # xmin
                    if cfg.a_quantize == 'parametric_pow2_xmin_xmax_relu':
                        g = k.replace('/xmin', '/xmax')
                        min_value = F.minimum2(p[k].data, p[g].data - 1e-5)
                        max_value = F.maximum2(p[k].data + 1e-5, p[g].data)
                        p[k].data = min_value
                        p[g].data = max_value
                    p[k].data = clip_scalar(p[k].data, cfg.a_xmin_min + 1e-5,
                                            cfg.a_xmin_max - 1e-5)
                elif k.endswith('/xmax'):  # xmax
                    p[k].data = clip_scalar(p[k].data, cfg.a_xmax_min + 1e-5,
                                            cfg.a_xmax_max - 1e-5)
コード例 #6
0
def volumetric_rendering(radiance_field,
                         ray_origins,
                         depth_values,
                         return_weights=False,
                         white_bkgd=False,
                         raw_noise_std=0.0,
                         apply_act=False):
    """Integration of volumetric rendering

    Args:
        radiance_field (nn.Variable or nn.NdArray): Shape is (height, width, num_samples, 4). 
        radiance_field[:,:,:,:3] correspond to rgb value at each sampled point while radiance_field[:,:,:,-1] refers to color density.
        ray_origins (nn.Variable or nn.NdArray): Shape is (height, width, 3)
        depth_values (nn.Variable or nn.NdArray): Shape is (num_samples, 1) or (height, width, num_samples) 
        return_weights (bool, optional): Set to true if the coefficients of the volumetric integration sum are to be returned . Defaults to False.

    Returns:
        rgb_map (nn.Variable or nn.NdArray): Shape is (height, width, 3)
        rgb_map (nn.Variable or nn.NdArray): Shape is (height, width, 1)
    """
    if apply_act:
        sigma = F.relu(radiance_field[..., 3])
        rgb = F.sigmoid(radiance_field[..., :3])
    else:
        sigma = radiance_field[..., 3]
        rgb = radiance_field[..., :3]

    if raw_noise_std > 0.0:
        noise = F.randn(shape=sigma.shape)
        sigma += (noise * raw_noise_std)

    if depth_values.ndim == 2:
        distances = depth_values[:, 1:] - depth_values[:, :-1]
        distances = F.concatenate(distances,
                                  F.constant(1e2,
                                             shape=depth_values.shape[:-1] +
                                             (1, )),
                                  axis=-1)
        alpha = 1. - F.exp(-sigma * distances)
        weights = alpha * F.cumprod(1 - alpha + 1e-10, axis=-1, exclusive=True)
        rgb_map = F.sum(weights[..., None] * rgb, axis=-2)
        depth_map = F.sum(weights * depth_values, axis=-1)
        acc_map = F.sum(weights, axis=-1)
    else:
        distances = depth_values[:, :, 1:] - depth_values[:, :, :-1]
        distances = F.concatenate(distances,
                                  F.constant(1e10,
                                             shape=depth_values.shape[:-1] +
                                             (1, )),
                                  axis=-1)
        alpha = 1. - F.exp(-sigma * distances)
        rgb_map = F.sum(weights[..., None] * rgb, axis=rgb.ndim - 2)
        depth_map = F.sum(weights * depth_values, axis=1)
        acc_map = F.sum(weights, axis=-1)

    if white_bkgd:
        rgb_map = rgb_map + (1. - acc_map[..., None])

    if return_weights:
        disp_map = 1.0 / \
            F.maximum2(F.constant(1e-10, depth_map.shape), depth_map / acc_map)
        return rgb_map, depth_map, acc_map, disp_map, weights

    return rgb_map, depth_map, acc_map