コード例 #1
0
ファイル: model.py プロジェクト: toannhu/TSNetVocoder
 def __init__(self, indim, outdim, normfac, fl=400, fs=80, fftl=512, fbsize=400):
     self.indim = indim
     self.outdim = outdim
     self.fl = fl
     self.fs = fs
     self.fftl = fftl
     self.fbsize = fbsize
     self.normfac = {'input'  : {'mean' : cuda.to_gpu(normfac['input']['mean']),
                                 'std' : cupy.fmax(cuda.to_gpu(normfac['input']['std']), 1.0E-6)},
                     'output' : {'mean' : cuda.to_gpu(normfac['output']['mean']),
                                 'std' : cupy.fmax(cuda.to_gpu(normfac['output']['std']), 1.0E-6)}}
     super(Model, self).__init__()
     with self.init_scope():
         self.lx1 = L.NStepBiLSTM(1, self.indim, self.indim//2, 0.0)
         self.lx2 = L.Convolution2D(1, self.indim, (5, self.indim), (1, 1), (2, 0))
         self.ly1 = L.NStepLSTM(3, self.fbsize+self.indim, 256, 0.0)
         self.ly2 = L.Linear(256, self.outdim)
コード例 #2
0
ファイル: spectralloss.py プロジェクト: toannhu/TSNetVocoder
 def _lossP(self, A, P):
     floor = 1.0E-6
     inum = cupy.array(1.0j, cupy.complex64)
     loss = (self.alpha * (1 - cupy.cos(P['y'] - P['x']))).mean()
     grad = self._overlapadd(
         cupy.fft.irfft(self.alpha * cupy.sin(P['y'] - P['x']) / cupy.fmax(
             A['x'], floor) * cupy.exp(inum * (P['x'] - 0.5 * cupy.pi))) *
         self.norm)
     return loss, grad
コード例 #3
0
ファイル: cifar10_cupy.py プロジェクト: heroverhrk/cma-es
for epoch in range(ngen):
    train_x, train_y = shuffle(train_x, train_y, random_state=random_state)
    for gen in range(n_batches):
        cp.cuda.Device(0).use()
        valid_y = cp.array(valid_y)
        network.gen_ind(population)
        network.conv_to_cp()
        start = gen * batch_size_ev
        end = start + batch_size_ev
        for i in range(start, end):
            train_x_cp = cp.array(train_x[i])
            train_y_cp = cp.array(train_y[i])
            y = cp.matmul(train_x_cp, network.W) + network.b
            y = cp.exp(y)
            y = y / (cp.sum(y, axis=1)[:, cp.newaxis])
            y = cp.fmax(nan_, y)
            network.cost += -cp.sum(train_y_cp * cp.log(y), axis=1)
        network.cost /= batch_size_ev
        network.selection()
        network.update_path(quantile)
        valid_y = cp.asnumpy(valid_y)
        #cp.cuda.Device(1).use()

        y = cp.matmul(valid_x_cp, network.W_top) + network.b_top
        y = cp.exp(y) / cp.sum(cp.exp(y), axis=1)[:, cp.newaxis]
        y = cp.fmax(1e-10, y)
        valid_cost = cp.mean(-cp.sum(valid_y_cp * cp.log(y), axis=1))
        y = cp.asnumpy(y)
        pred_y = np.argmax(y, 1).astype('int32')
        f1 = f1_score(np.argmax(valid_y, 1).astype('int32'),
                      pred_y.astype('int32'),
コード例 #4
0
def relu_(x):
    return cp.fmax(x, 0)
コード例 #5
0
        for i in range(start, end):
            train_x_cp = cp.array(train_x[i])
            train_y_cp = cp.array(train_y[i])
            y = relu_(cp.matmul(train_x_cp, dense1.W) + dense1.b)
            y = relu_(cp.matmul(y, W2) + b2)
            y = relu_(cp.matmul(y, W3) + b3)
            y = relu_(cp.matmul(y, W4) + b4)
            y = relu_(cp.matmul(y, W5) + b5)
            y = relu_(cp.matmul(y, W6) + b6)
            y = relu_(cp.matmul(y, W7) + b7)
            y = relu_(cp.matmul(y, W8) + b8)
            y = relu_(cp.matmul(y, W9) + b9)
            y = cp.matmul(y, W10) + b10
            y = cp.exp(y) / cp.sum(cp.exp(y), axis=1)[:, cp.newaxis]
            y = cp.fmax(1e-10, y)
            dense1.cost += -cp.sum(train_y_cp*cp.log(y), axis=1)
        dense1.cost /= batch_size_ev
        dense1.selection()
        dense1.update_path(quantile)

        W1 = cp.array(dense1.W_top)
        b1 = cp.array(dense1.b_top)

        valid_x_cp = cp.array(valid_x)
        valid_y_cp = cp.array(valid_y)
        valid_y = cp.asnumpy(valid_y)
        y = relu_(cp.matmul(valid_x_cp, W1) + b1)
        y = relu_(cp.matmul(y, W2) + b2)
        y = relu_(cp.matmul(y, W3) + b3)
        y = relu_(cp.matmul(y, W4) + b4)
コード例 #6
0
def cross_correlate_masked(arr1,
                           arr2,
                           m1,
                           m2,
                           mode="full",
                           axes=(-2, -1),
                           overlap_ratio=0.3):
    """
    Masked normalized cross-correlation between arrays.

    Parameters
    ----------
    arr1 : ndarray
        First array.
    arr2 : ndarray
        Seconds array. The dimensions of `arr2` along axes that are not
        transformed should be equal to that of `arr1`.
    m1 : ndarray
        Mask of `arr1`. The mask should evaluate to `True`
        (or 1) on valid pixels. `m1` should have the same shape as `arr1`.
    m2 : ndarray
        Mask of `arr2`. The mask should evaluate to `True`
        (or 1) on valid pixels. `m2` should have the same shape as `arr2`.
    mode : {'full', 'same'}, optional
        'full':
            This returns the convolution at each point of overlap. At
            the end-points of the convolution, the signals do not overlap
            completely, and boundary effects may be seen.
        'same':
            The output is the same size as `arr1`, centered with respect
            to the `‘full’` output. Boundary effects are less prominent.
    axes : tuple of ints, optional
        Axes along which to compute the cross-correlation.
    overlap_ratio : float, optional
        Minimum allowed overlap ratio between images. The correlation for
        translations corresponding with an overlap ratio lower than this
        threshold will be ignored. A lower `overlap_ratio` leads to smaller
        maximum translation, while a higher `overlap_ratio` leads to greater
        robustness against spurious matches due to small overlap between
        masked images.

    Returns
    -------
    out : ndarray
        Masked normalized cross-correlation.

    Raises
    ------
    ValueError : if correlation `mode` is not valid, or array dimensions along
        non-transformation axes are not equal.

    References
    ----------
    .. [1] Dirk Padfield. Masked Object Registration in the Fourier Domain.
           IEEE Transactions on Image Processing, vol. 21(5),
           pp. 2706-2718 (2012). :DOI:`10.1109/TIP.2011.2181402`
    .. [2] D. Padfield. "Masked FFT registration". In Proc. Computer Vision and
           Pattern Recognition, pp. 2918-2925 (2010).
           :DOI:`10.1109/CVPR.2010.5540032`
    """
    if mode not in {"full", "same"}:
        raise ValueError("Correlation mode {} is not valid.".format(mode))

    if arr1.dtype.kind == "c" or arr2.dtype.kind == "c":
        raise ValueError("complex-valued arr1, arr2 are not supported")
    fixed_image = cp.asarray(arr1, dtype=np.float)
    fixed_mask = cp.asarray(m1, dtype=np.bool)
    moving_image = cp.asarray(arr2, dtype=np.float)
    moving_mask = cp.asarray(m2, dtype=np.bool)
    eps = np.finfo(np.float).eps

    # Array dimensions along non-transformation axes should be equal.
    all_axes = set(range(fixed_image.ndim))
    for axis in all_axes - set(axes):
        if fixed_image.shape[axis] != moving_image.shape[axis]:
            raise ValueError(
                "Array shapes along non-transformation axes should be "
                "equal, but dimensions along axis {a} are not".format(a=axis))

    # Determine final size along transformation axes
    # Note that it might be faster to compute Fourier transform in a slightly
    # larger shape (`fast_shape`). Then, after all fourier transforms are done,
    # we slice back to`final_shape` using `final_slice`.
    final_shape = list(arr1.shape)
    for axis in axes:
        final_shape[axis] = (fixed_image.shape[axis] +
                             moving_image.shape[axis] - 1)
    final_shape = tuple(final_shape)
    final_slice = tuple([slice(0, int(sz)) for sz in final_shape])

    # Extent transform axes to the next fast length (i.e. multiple of 3, 5, or
    # 7)
    fast_shape = tuple([next_fast_len(final_shape[ax]) for ax in axes])

    # We use numpy.fft or the new scipy.fft because they allow leaving the
    # transform axes unchanged which was not possible with scipy.fftpack's
    # fftn/ifftn in older versions of SciPy.
    # E.g. arr shape (2, 3, 7), transform along axes (0, 1) with shape (4, 4)
    # results in arr_fft shape (4, 4, 7)
    fft = partial(fftmodule.fftn, s=fast_shape, axes=axes)
    ifft = partial(fftmodule.ifftn, s=fast_shape, axes=axes)

    fixed_image[cp.logical_not(fixed_mask)] = 0.0
    moving_image[cp.logical_not(moving_mask)] = 0.0

    # N-dimensional analog to rotation by 180deg is flip over all relevant axes.
    # See [1] for discussion.
    rotated_moving_image = _flip(moving_image, axes=axes)
    rotated_moving_mask = _flip(moving_mask, axes=axes)

    fixed_fft = fft(fixed_image)
    rotated_moving_fft = fft(rotated_moving_image)
    fixed_mask_fft = fft(fixed_mask)
    rotated_moving_mask_fft = fft(rotated_moving_mask)

    # Calculate overlap of masks at every point in the convolution.
    # Locations with high overlap should not be taken into account.
    number_overlap_masked_px = cp.real(
        ifft(rotated_moving_mask_fft * fixed_mask_fft))
    number_overlap_masked_px[:] = cp.around(number_overlap_masked_px)
    number_overlap_masked_px[:] = cp.fmax(number_overlap_masked_px, eps)
    masked_correlated_fixed_fft = ifft(rotated_moving_mask_fft * fixed_fft)
    masked_correlated_rotated_moving_fft = ifft(fixed_mask_fft *
                                                rotated_moving_fft)

    numerator = ifft(rotated_moving_fft * fixed_fft)
    numerator -= (masked_correlated_fixed_fft *
                  masked_correlated_rotated_moving_fft /
                  number_overlap_masked_px)

    fixed_squared_fft = fft(cp.square(fixed_image))
    fixed_denom = ifft(rotated_moving_mask_fft * fixed_squared_fft)
    fixed_denom -= (cp.square(masked_correlated_fixed_fft) /
                    number_overlap_masked_px)
    fixed_denom[:] = cp.fmax(fixed_denom, 0.0)

    rotated_moving_squared_fft = fft(cp.square(rotated_moving_image))
    moving_denom = ifft(fixed_mask_fft * rotated_moving_squared_fft)
    moving_denom -= (cp.square(masked_correlated_rotated_moving_fft) /
                     number_overlap_masked_px)
    moving_denom[:] = cp.fmax(moving_denom, 0.0)

    denom = cp.sqrt(fixed_denom * moving_denom)

    # Slice back to expected convolution shape.
    numerator = numerator[final_slice]
    denom = denom[final_slice]
    number_overlap_masked_px = number_overlap_masked_px[final_slice]

    if mode == "same":
        _centering = partial(_centered, newshape=fixed_image.shape, axes=axes)
        denom = _centering(denom)
        numerator = _centering(numerator)
        number_overlap_masked_px = _centering(number_overlap_masked_px)

    # Pixels where `denom` is very small will introduce large
    # numbers after division. To get around this problem,
    # we zero-out problematic pixels.
    tol = 1e3 * eps * cp.max(cp.abs(denom), axis=axes, keepdims=True)
    nonzero_indices = denom > tol

    # TODO: grlee77: Added a cast to real here.
    #                probably it should be real earlier?
    numerator = numerator.real
    denom = denom.real
    out = cp.zeros_like(denom)
    out[nonzero_indices] = numerator[nonzero_indices] / denom[nonzero_indices]
    cp.clip(out, a_min=-1, a_max=1, out=out)

    # Apply overlap ratio threshold
    number_px_threshold = overlap_ratio * cp.max(
        number_overlap_masked_px, axis=axes, keepdims=True)
    out[number_overlap_masked_px < number_px_threshold] = 0.0

    return out