示例#1
0
def _test_clamp_scalar_max(test_case, shape, device):
    input = flow.tensor(np.random.randn(*shape),
                        dtype=flow.float32,
                        device=flow.device(device))
    of_out = flow.clamp(input, None, 0.5)
    np_out = np.clip(input.numpy(), None, 0.5)
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-05, 1e-05))
示例#2
0
    def forward(self, inputs, targets):
        """
        Args:
            inputs (torch.Tensor): feature matrix with shape (batch_size, feat_dim).
            targets (torch.LongTensor): ground truth labels with shape (num_classes).
        """
        n = inputs.size(0)

        # Compute pairwise distance, replace by the official when merged
        dist = flow.pow(inputs, 2).sum(dim=1).expand(n, n)
        dist = dist + flow.transpose(dist, dim0=1, dim1=0)
        temp1 = -2 * flow.matmul(inputs, flow.transpose(inputs, dim0=1,
                                                        dim1=0))
        dist = flow.add(dist, temp1)
        dist = flow.sqrt(flow.clamp(dist, min=1e-12))
        # For each anchor, find the hardest positive and negative
        mask = targets.expand(n, n).eq(
            flow.transpose(targets.expand(n, n), dim0=1, dim1=0))
        dist_ap, dist_an = [], []
        y1 = flow.zeros((1, n), dtype=flow.float32).to("cuda")
        y2 = flow.Tensor(np.exp(100 * np.ones((1, n)))).to("cuda")

        for i in range(n):
            temp_dist = flow.slice(dist, [(i, i + 1, 1)])
            temp_mask = flow.slice(mask, [(i, i + 1, 1)])
            temp_mask_rev = flow.slice(1 - mask, [(i, i + 1, 1)])
            dist_ap.append(temp_mask.where(temp_dist, y1).max().unsqueeze(0))
            dist_an.append(
                temp_mask_rev.where(temp_dist, y2).min().unsqueeze(0))
        dist_ap = flow.cat(dist_ap)
        dist_an = flow.cat(dist_an)

        # Compute ranking hinge loss
        y = flow.ones_like(dist_an)
        return self.ranking_loss(dist_an, dist_ap, y)
示例#3
0
文件: losses.py 项目: kanagi2587/test
 def build(self,inputs,targets):
     n=inputs.shape[0]
     if self.distance=='euclidean':
         dist=flow.math.pow(inputs,2)
         dist=flow.math.reduce_sum(dist, axis=1, keepdims=True)
         dist=np.tile(dist,(n, n))
         dist_t=flow.transpose(dist)
         dist=dist+dist_t
         inputs_t=flow.transpose(inputs)
         dist=addmm(dist,inputs,inputs_t,beta=1,alpha=-2)
         dist=flow.clamp(min_value=1e-12)
         dist=flow.math.sqrt(dist)
     elif self.distance == 'cosine':
         fnorm=np.linalg.norm(inputs,ord=2,axis=1,keepdims=True)
         l2norm=np.tile(inputs,(inputs.shape))
         l2norm=inputs/l2norm
         l2norm_t=flow.transpose(l2norm)
         dist=-np.matmul(l2norm,l2norm_t)
     target_expand=np.tile(targets,(n,n))
     target_expand_t=flow.transpose(target_expand)
     mask=flow.math.equal(target_expand,target_expand_t)
     dist_ap, dist_an = [], []
     for i in range(n):
         temp=np.ndarray.max(dist[i][mask[i]])
         temp=flow.expand_dims(temp,axis=0)
         dist_ap.append(temp)
         temp=np.ndarray.min(dist[i][mask[i]==0])
         temp=flow.expand_dims(temp,axis=0)
         dist_an.append(temp)
         dist_ap=flow.concat(dist_ap)
         dist_an=flow.concat(dist_an)
     y=flow.ones_like(dist_an)
     loss=self.ranking_loss(dist_an, dist_ap, y,margin=self.margin)
     return loss
示例#4
0
def styleNet(input, trainable=True):
    with flow.scope.namespace("style_transfer"):
        # Initial convolution layers
        conv1 = conv2d_layer("first_conv",
                             input,
                             32,
                             kernel_size=9,
                             strides=1,
                             trainable=trainable)
        in1 = instance_norm(conv1, "first_conv_in", trainable=trainable)
        in1 = flow.nn.relu(in1)
        conv2 = conv2d_layer("second_conv",
                             in1,
                             64,
                             kernel_size=3,
                             strides=2,
                             trainable=trainable)
        in2 = instance_norm(conv2, "second_conv_in", trainable=trainable)
        in2 = flow.nn.relu(in2)
        conv3 = conv2d_layer("third_conv",
                             in2,
                             128,
                             kernel_size=3,
                             strides=2,
                             trainable=trainable)
        in3 = instance_norm(conv3, "third_conv_in", trainable=trainable)
        in3 = flow.nn.relu(in3)
        # Residual layers
        res1 = resBlock(in3, 128, "res1", trainable=trainable)
        res2 = resBlock(res1, 128, "res2", trainable=trainable)
        res3 = resBlock(res2, 128, "res3", trainable=trainable)
        res4 = resBlock(res3, 128, "res4", trainable=trainable)
        res5 = resBlock(res4, 128, "res5", trainable=trainable)
        # Upsampling Layers
        upsample1 = upsampleConvLayer(res5,
                                      "upsample1",
                                      64,
                                      3,
                                      trainable=trainable)
        # upsample1 = deconv(res5, 64, "upsample1", kernel_size = 4, strides = [2, 2], trainable = True)
        in4 = instance_norm(upsample1, "upsample1_in", trainable=trainable)
        in4 = flow.nn.relu(in4)
        upsample2 = upsampleConvLayer(in4,
                                      "upsample2",
                                      32,
                                      3,
                                      trainable=trainable)
        # upsample2 = deconv(in4, 32, "upsample2", kernel_size = 4, strides = [2, 2], trainable = True)
        in5 = instance_norm(upsample2, "upsample2_in", trainable=trainable)
        in5 = flow.nn.relu(in5)
        conv1 = conv2d_layer("last_conv",
                             in5,
                             3,
                             kernel_size=9,
                             strides=1,
                             trainable=trainable)
        out = flow.clamp(conv1, 0, 255)
    return out
示例#5
0
    def build(self, inputs, targets):
        """
        Args:
            inputs (torch.Tensor): feature matrix with shape (batch_size, feat_dim).
            targets (torch.LongTensor): ground truth labels with shape (num_classes).
        """
        n = inputs.shape[0]
        dist = math.reduce_sum(math.pow(
            inputs, flow.constant_like(inputs, 2, dtype=flow.float32)),
                               axis=1)
        shape_tensor = flow.constant(value=0.0,
                                     dtype=flow.float32,
                                     shape=(n, n))
        dist = flow.broadcast_like(dist, like=shape_tensor, broadcast_axes=[1])
        dist = math.add(
            dist, flow.transpose(dist, perm=(1, 0),
                                 batch_axis_non_change=True))
        temp1 = math.multiply(
            -2,
            flow.matmul(
                inputs,
                flow.transpose(inputs, perm=(1, 0),
                               batch_axis_non_change=True)))
        dist = math.add(dist, temp1)
        dist = math.sqrt(flow.clamp(dist, min_value=1e-12))
        mask = math.equal(
            flow.broadcast_like(targets, like=shape_tensor,
                                broadcast_axes=[1]),
            flow.transpose(flow.broadcast_like(targets,
                                               like=shape_tensor,
                                               broadcast_axes=[1]),
                           perm=(1, 0),
                           batch_axis_non_change=True))
        mask_rev = math.not_equal(
            flow.broadcast_like(targets, like=shape_tensor,
                                broadcast_axes=[1]),
            flow.transpose(flow.broadcast_like(targets,
                                               like=shape_tensor,
                                               broadcast_axes=[1]),
                           perm=(1, 0),
                           batch_axis_non_change=True))
        dist_ap, dist_an = [], []
        for i in range(n):
            temp_dist = flow.slice_v2(dist, [(i, i + 1, 1)])
            temp_mask = flow.slice_v2(mask, [(i, i + 1, 1)])
            temp_mask_rev = flow.slice_v2(mask_rev, [(i, i + 1, 1)])
            dist_ap.append(
                math.reduce_max(
                    flow.gather_nd(temp_dist, flow.where(temp_mask))))
            dist_an.append(
                math.reduce_min(
                    flow.gather_nd(temp_dist, flow.where(temp_mask_rev))))
        dist_ap = flow.concat(dist_ap, 0)
        dist_an = flow.concat(dist_an, 0)
        y = flow.ones_like(dist_an)
        # return dist_an, dist_ap, y

        return self._MarginRankingLoss(dist_an, dist_ap, y)
示例#6
0
def _trunc_normal_(
    self,
    mean=0.0,
    std=1.0,
    a=-2.0,
    b=2.0,
):
    initializer_conf = flow.truncated_normal_initializer(mean=mean, stddev=std)
    res = _init_by_initializer_conf(self, initializer_conf)
    res = flow.clamp(res, min=a, max=b)
    return res
示例#7
0
def _test_clamp_backward(test_case, shape, device):
    x = flow.tensor(
        np.random.randn(*shape),
        dtype=flow.float32,
        device=flow.device(device),
        requires_grad=True,
    )
    y = flow.clamp(x, 0.1, 0.5).sum()
    y.backward()
    test_case.assertTrue(
        np.allclose(x.grad.numpy(), _numpy_clamp_grad(x.numpy(), 0.1, 0.5),
                    1e-05, 1e-05))
示例#8
0
 def forward(self, X):
     y = self.relu(self.in1(self.conv1(X)))
     y = self.relu(self.in2(self.conv2(y)))
     y = self.relu(self.in3(self.conv3(y)))
     y = self.res1(y)
     y = self.res2(y)
     y = self.res3(y)
     y = self.res4(y)
     y = self.res5(y)
     y = self.relu(self.in4(self.deconv1(y)))
     y = self.relu(self.in5(self.deconv2(y)))
     y = self.deconv3(y)
     y = flow.clamp(y, 0, 255)
     return y
示例#9
0
    def forward(self, waveforms):
        """
        Parameters
        ----------
        waveforms : `oneflow.Tensor` (batch_size, 1, n_samples)
            Batch of waveforms.
        Returns
        -------
        features : `oneflow.Tensor` (batch_size, out_channels, n_samples_out)
            Batch of sinc filters activations.
        """
        self.n_ = self.n_.to(waveforms.device)

        self.window_ = self.window_.to(waveforms.device)

        low = self.min_low_hz + flow.abs(self.low_hz_)

        high = flow.clamp(
            low + self.min_band_hz + flow.abs(self.band_hz_),
            self.min_low_hz,
            self.sample_rate / 2,
        )
        band = (high - low)[:, 0]

        f_times_t_low = flow.matmul(low, self.n_)
        f_times_t_high = flow.matmul(high, self.n_)

        band_pass_left = (
            (flow.sin(f_times_t_high) - flow.sin(f_times_t_low)) / (self.n_ / 2)
        ) * self.window_
        band_pass_center = 2 * band.reshape(-1, 1)
        band_pass_right = flow.flip(band_pass_left, dims=[1])

        band_pass = flow.cat([band_pass_left, band_pass_center, band_pass_right], dim=1)

        band_pass = band_pass / (2 * band[:, None])

        self.filters = (band_pass).reshape(self.out_channels, 1, self.kernel_size)

        output = F.conv1d(
            waveforms,
            self.filters,
            stride=[self.stride],
            padding=[self.padding],
            dilation=[self.dilation],
            bias=None,
            groups=1,
        )
        return output
示例#10
0
def MergeTail(x1, x2, x3, trainable=True):
    x13 = flow.layers.upsample_2d(x1, (4, 4),
                                  interpolation='bilinear',
                                  name='upsanple7')
    x13 = conv1x1(x13, 64, "MergeTail_0", trainable=trainable)
    x13 = flow.math.relu(x13)
    x23 = flow.layers.upsample_2d(x2, (2, 2),
                                  interpolation='bilinear',
                                  name='upsanple8')
    x23 = conv1x1(x23, 64, "MergeTail_1", trainable=trainable)
    x23 = flow.math.relu(x23)

    x = conv3x3(flow.concat((x3, x13, x23), axis=1),
                64,
                "MergeTail_2",
                trainable=trainable)
    x = flow.math.relu(x)
    x = conv3x3(x, 32, "MergeTail_3", trainable=trainable)
    x = conv1x1(x, 3, "MergeTail_4", trainable=trainable)
    x = flow.clamp(x, -1, 1)

    return x
示例#11
0
def _test_clamp_integral(test_case, shape, device):
    input = flow.tensor(np.random.randint(3, 10, shape),
                        device=flow.device(device))
    of_out = flow.clamp(input, 1, 5)
    np_out = np.clip(input.numpy(), 1, 5)
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-05, 1e-05))
示例#12
0
    def forward(self, inputs, targets):
        n = inputs.shape[0]
        # Compute pairwise distance, replace by the official when merged
        tempname = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S.%f')
        shape_tensor = flow.constant(value=0.0,
                                     dtype=flow.float32,
                                     shape=(n, n))
        if self.distance == 'euclidean':
            blob_2 = flow.get_variable(
                "blob_2_" + tempname,
                shape=inputs.shape,
                initializer=flow.constant_initializer(2),
                dtype=inputs.dtype)
            dist = flow.math.pow(inputs, blob_2)

            dist = flow.math.reduce_sum(dist, axis=1, keepdims=True)
            dist = flow.broadcast_like(dist, shape_tensor)
            tempdist = flow.transpose(dist)
            dist = dist + tempdist
            inputs_t = flow.transpose(inputs)
            dist = addmm(dist, inputs, inputs_t, beta=1, alpha=-2)
            dist = flow.clamp(dist, min_value=1e-12)
            dist = flow.math.sqrt(dist)
        elif self.distance == 'cosine':
            #fnorm=flow.math.l2_normalize(inputs, axis=1)
            fnorm = flow.math.reduce_mean(flow.math.divide(
                inputs, flow.math.l2_normalize(inputs, axis=1)),
                                          axis=1,
                                          keepdims=True)

            expand_fnorm = flow.broadcast_like(fnorm,
                                               like=inputs,
                                               broadcast_axes=[1])
            l2norm = flow.math.divide(inputs, expand_fnorm)
            l2norm_t = flow.transpose(l2norm, perm=(1, 0))
            dist = flow.math.negative(flow.matmul(l2norm, l2norm_t))
        # For each anchor, find the hardest positive and negative
        mask = math.equal(
            flow.broadcast_like(targets, like=shape_tensor,
                                broadcast_axes=[1]),
            flow.transpose(flow.broadcast_like(targets,
                                               like=shape_tensor,
                                               broadcast_axes=[1]),
                           perm=(1, 0),
                           batch_axis_non_change=True))
        mask_rev = math.not_equal(
            flow.broadcast_like(targets, like=shape_tensor,
                                broadcast_axes=[1]),
            flow.transpose(flow.broadcast_like(targets,
                                               like=shape_tensor,
                                               broadcast_axes=[1]),
                           perm=(1, 0),
                           batch_axis_non_change=True))
        dist_ap, dist_an = [], []
        for i in range(n):
            temp_dist = flow.slice_v2(dist, [(i, i + 1, 1)])
            temp_mask = flow.slice_v2(mask, [(i, i + 1, 1)])
            temp_mask_rev = flow.slice_v2(mask_rev, [(i, i + 1, 1)])
            temp_dist_ap = flow.expand_dims(
                math.reduce_max(
                    flow.gather_nd(temp_dist, flow.where(temp_mask))), 0)
            temp_dist_an = flow.expand_dims(
                math.reduce_min(
                    flow.gather_nd(temp_dist, flow.where(temp_mask_rev))), 0)
            dist_ap.append(temp_dist_ap)
            dist_an.append(temp_dist_an)
        dist_ap = flow.concat(dist_ap, 0)
        dist_an = flow.concat(dist_an, 0)
        y = flow.ones_like(dist_an)
        return self._MarginRankingLoss(dist_an, dist_ap, y)