Ejemplo n.º 1
0
    def forward(self, data, weight, mapping_label, depth):
        """
        """
        with autograd.record():
            norm_data = nd.L2Normalization(data)
            norm_weight = nd.L2Normalization(weight)
            #
            fc7 = nd.dot(norm_data, norm_weight, transpose_b=True)
            #
            mapping_label_onehot = mx.nd.one_hot(indices=mapping_label,
                                                 depth=depth,
                                                 on_value=1.0,
                                                 off_value=0.0)
            # cosface
            if self.loss_m1 == 1.0 and self.loss_m2 == 0.0:
                _one_hot = mapping_label_onehot * self.loss_m3
                fc7 = fc7 - _one_hot
            elif self.loss_m1 == 1.0 and self.loss_m3 == 0.0:
                fc7_onehot = fc7 * mapping_label_onehot
                cos_t = fc7_onehot
                t = nd.arccos(cos_t)
                if self.loss_m1 != 1.0:
                    t = t * self.loss_m1
                if self.loss_m2 != 0.0:
                    t = t + self.loss_m2
                margin_cos = nd.cos(t)
                if self.loss_m3 != 0.0:
                    margin_cos = margin_cos - self.loss_m3
                margin_fc7 = margin_cos
                margin_fc7_onehot = margin_fc7 * mapping_label_onehot
                diff = margin_fc7_onehot - fc7_onehot
                fc7 = fc7 + diff
            else:
                cosine = fc7
                sine = nd.sqrt(1 - fc7 * fc7)
                m = nd.array([self.loss_m2], ctx=fc7.context)
                # phi = cosine * nd.cos(m) - sine * nd.sin(m)
                cos_t = fc7_onehot
                t = nd.arccos(cos_t)
                phi = nd.cos(t + self.loss_m2)
                mask = cosine > phi
                print('mask', mask.shape)
                hard_example = nd.where(cosine > phi, cosine)
                self.t = self.t.as_in_context(fc7.context)
                self.t = cosine * mapping_label_onehot.mean() * 0.01 + (
                    1 - 0.01) * self.t
                print("cosine", cosine.shape)
                print(self.t.shape)
                print('dasdasdasdad', hard_example.shape)
                cosine[mask] = hard_example * (self.t + hard_example)
                fc7 = mapping_label_onehot * phi + cosine * (
                    1.0 - mapping_label_onehot)

            fc7 = fc7 * self.loss_s
            return fc7, mapping_label_onehot
Ejemplo n.º 2
0
def bgr2hsi(x):
    """ x:n,c(b,g,r),w,h
        return n,c(h,s,i),w,h
    """
    sum_RGB = nd.sum(x.astype('float32'), axis=1)
    R = x[:, 0, :, :].astype('float32')
    G = x[:, 1, :, :].astype('float32')
    B = x[:, 2, :, :].astype('float32')

    r = (R + eps) / (sum_RGB + 3 * eps)
    g = (G + eps) / (sum_RGB + 3 * eps)
    b = (B + eps) / (sum_RGB + 3 * eps)

    cossita = (2 * r - g - b) / (2 * ((r - g)**2 + (r - b) *
                                      (g - b))**(1.0 / 2) + eps)
    cossita_cilp = nd.clip(cossita, -1.0, 1.0)

    sita = nd.arccos(cossita_cilp)

    h = (nd.where(g >= b, sita, 2 * math.pi - sita)).expand_dims(axis=1)

    s = (1 - 3 * nd.minimum(nd.minimum(r, g), b)).expand_dims(axis=1)
    s = nd.clip(s, 0., 1.)

    i = ((R + G + B) / 3).expand_dims(axis=1)

    return nd.concat(h, s, i, dim=1)
 def implement_1(self, x, label):
     '''
     following paper to implement
     '''
     #  weight normalize
     with x.context:
         w = self.weight.data()
     w_norm = w / nd.sqrt(nd.sum(nd.power(w, 2), axis=1)).reshape((-1, 1))
     #  cos_theta = x'w/|x|. note: |w| = 1
     x_norm = nd.power(x, 2)
     x_norm = nd.sum(x_norm, axis=1)
     x_norm = nd.sqrt(x_norm)
     cos_theta = nd.dot(x, w_norm, transpose_b=True)
     cos_theta = cos_theta / x_norm.reshape((-1, 1))
     cos_theta = nd.clip(cos_theta, -1, 1)
     #  cos_m_theta = cos(m * theta)
     cos_m_theta = self.margin_cos[self.margin](cos_theta)
     #  k
     with mx.autograd.pause():
         theta = nd.arccos(cos_theta)
         k = nd.sign((self.margin * theta / math.pi))
     #  i=j is phi_theta and i!=j is cos_theta
     phi_theta = ((-1)**k) * cos_m_theta - 2 * k
     x_norm_phi_theta = x_norm.reshape((-1, 1)) * phi_theta
     x_norm_cos_theta = x_norm.reshape((-1, 1)) * cos_theta
     #  i=j index
     with mx.autograd.pause():
         index = nd.one_hot(label, x_norm_phi_theta.shape[1])
     #  output
     with mx.autograd.pause():
         lamb = self.__get_lambda()
     output = x_norm_cos_theta * 1.0
     output = output - x_norm_cos_theta * index / (1 + lamb)
     output = output + x_norm_phi_theta * index / (1 + lamb)
     return output
Ejemplo n.º 4
0
    def forward(self, data, weight, mapping_label, depth):
        """
        """
        with autograd.record():
            norm_data = nd.L2Normalization(data)
            norm_weight = nd.L2Normalization(weight)
            #
            fc7 = nd.dot(norm_data, norm_weight, transpose_b=True)
            #
            mapping_label_onehot = mx.nd.one_hot(indices=mapping_label,
                                                 depth=depth,
                                                 on_value=1.0,
                                                 off_value=0.0)
            # cosface
            if self.loss_m1 == 1.0 and self.loss_m2 == 0.0:
                _one_hot = mapping_label_onehot * self.loss_m3
                fc7 = fc7 - _one_hot
            else:
                fc7_onehot = fc7 * mapping_label_onehot
                cos_t = fc7_onehot
                t = nd.arccos(cos_t)
                if self.loss_m1 != 1.0:
                    t = t * self.loss_m1
                if self.loss_m2 != 0.0:
                    t = t + self.loss_m2
                margin_cos = nd.cos(t)
                if self.loss_m3 != 0.0:
                    margin_cos = margin_cos - self.loss_m3
                margin_fc7 = margin_cos
                margin_fc7_onehot = margin_fc7 * mapping_label_onehot
                diff = margin_fc7_onehot - fc7_onehot
                fc7 = fc7 + diff

            fc7 = fc7 * self.loss_s
            return fc7, mapping_label_onehot
    def cart2sph(self, n):
        temp = n[1] / n[0]

        if n[0] == 0:
            if n[1] < 0:
                phi = -nd.pi / 2
            else:
                phi = nd.pi / 2
        else:
            if n[0] > 0:
                phi = nd.arctan(temp)
            elif n[1] < 0:
                phi = nd.arctan(temp) - np.pi
            else:
                phi = nd.arctan(temp) + np.pi

    #     phi = np.arctan() #arctan(y/x)
        theta = nd.arccos(n[2])  #arccos(z)

        return [phi, theta]
Ejemplo n.º 6
0
    def hybrid_forward(self, F, x, *args, **params):
        # xsize=(B,F)    F is feature len
        w = self.weight._data[0]  # size=(Classnum,F) F=in_features Classnum=out_features

        ww = nd.L2Normalization(w)
        xlen = x.square().sum(axis=1, keepdims=True).sqrt()
        wlen = ww.square().sum(axis=1, keepdims=True).sqrt()

        cos_theta = nd.dot(x, ww.T) / xlen.reshape(-1, 1) / wlen.reshape(1, -1)
        cos_theta = cos_theta.clip(-1, 1)

        cos_m_theta = self.mlambda[self.m](cos_theta)
        theta = nd.arccos(cos_theta)
        k = (self.m * theta / math.pi).floor()
        phi_theta = (-1 ** k) * cos_m_theta - 2 * k

        cos_theta = cos_theta * xlen.reshape(-1, 1)
        phi_theta = phi_theta * xlen.reshape(-1, 1)
        output = (cos_theta, phi_theta)
        return output  # size=(B,Classnum,2)
Ejemplo n.º 7
0
def compute_eigenvals(A):
    A_11 = A[:, :, 0, 0]  # (N, P)
    A_12 = A[:, :, 0, 1]
    A_13 = A[:, :, 0, 2]
    A_22 = A[:, :, 1, 1]
    A_23 = A[:, :, 1, 2]
    A_33 = A[:, :, 2, 2]
    I = nd.eye(3)
    p1 = nd.square(A_12) + nd.square(A_13) + nd.square(A_23)  # (N, P)
    q = (A_11 + A_22 + A_33) / 3  # (N, P)
    p2 = nd.square(A_11 - q) + nd.square(A_22 -
                                         q) + nd.square(A_33 -
                                                        q) + 2 * p1  # (N, P)
    p = nd.sqrt(p2 / 6) + 1e-8  # (N, P)
    N = A.shape[0]
    q_4d = nd.reshape(q, (N, -1, 1, 1))  # (N, P, 1, 1)
    p_4d = nd.reshape(p, (N, -1, 1, 1))
    B = (1 / p_4d) * (A - q_4d * I)  # (N, P, 3, 3)
    r = nd.clip(compute_determinant(B) / 2, -1, 1)  # (N, P)
    phi = nd.arccos(r) / 3  # (N, P)
    eig1 = q + 2 * p * nd.cos(phi)  # (N, P)
    eig3 = q + 2 * p * nd.cos(phi + (2 * math.pi / 3))
    eig2 = 3 * q - eig1 - eig3
    return nd.abs(nd.stack([eig1, eig2, eig3], axis=2))  # (N, P, 3)
Ejemplo n.º 8
0
 def arccos(x):
     return nd.arccos(x)
 def check_arccos():
     x = create_input_for_trigonometric_ops([-1, -.707, 0, .707, 1])
     y = nd.arccos(x)
     # expected ouput for indices=(0, 1, -3, -2, -1) after applying arccos()
     expected_output = [np.pi, 3 * np.pi / 4, np.pi / 2, np.pi / 4, 0]
     assert_correctness_of_trigonometric_ops(y, expected_output)
Ejemplo n.º 10
0
 def diffusion_kernel(a, tmpt, dim):
     # return (4 * np.pi * tmpt)**(-dim / 2) * nd.exp(- nd.square(nd.arccos(a)) / tmpt)
     return nd.exp(- nd.square(nd.arccos(a)) / tmpt)
Ejemplo n.º 11
0
def flowdr(dem_fill,NoData,rows,cols,ctx,switch):
    ingrid = np.indices((rows, cols))
    ingrid[0]        # row indices
    ingrid[1]        # column indices
    ingridxmx=nd.array(ingrid[1],ctx[0]).reshape((1,1,rows, cols))
    ingridymx=nd.array(ingrid[0],ctx[0]).reshape((1,1,rows, cols))
    dem_fillmx=nd.array(dem_fill,ctx[0])
    demmx=dem_fillmx.reshape((1,1,rows, cols))
    res=1
    l=[0,1,2,3,4,5,6,7,0]
    direct=[1,2,4,8,16,32,64,128]
    direct_d=[[1,3],[2,6],[4,12],[8,24],[16,48],[32,96],[64,192],[128,129]]
    weight=[None]*8
    weight1=[None]*8
    convx=[None]*8
    convy=[None]*8
    convz=[None]*8
    runlen=[1,ma.pow(2,0.5),1,ma.pow(2,0.5),1,ma.pow(2,0.5),1,ma.pow(2,0.5)]*res
    n = [[[] for x in range(3)] for x in range(8)]#create list to store normal vectors for each facet
    s = [None]*8
    d = [None]*8

    weight[0] = nd.array([[0, 0, 0], [0, 1, -1], [0, 0, 0]], gpu(0))
    weight[1] = nd.array([[0, 0, -1], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight[2] = nd.array([[0, -1, 0], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight[3] = nd.array([[-1, 0, 0], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight[4] = nd.array([[0, 0, 0], [-1, 1, 0], [0, 0, 0]], gpu(0))
    weight[5] = nd.array([[0, 0, 0], [0, 1, 0], [-1, 0, 0]], gpu(0))
    weight[6] = nd.array([[0, 0, 0], [0, 1, 0], [0, -1, 0]], gpu(0))
    weight[7] = nd.array([[0, 0, 0], [0, 1, 0], [0, 0, -1]], gpu(0))
    
    weight1[0] = nd.array([[0, 0, 0], [0, 1, -10], [0, 0, 0]], gpu(0))
    weight1[1] = nd.array([[0, 0, -10], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight1[2] = nd.array([[0, -10, 0], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight1[3] = nd.array([[-10, 0, 0], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight1[4] = nd.array([[0, 0, 0], [-10, 1, 0], [0, 0, 0]], gpu(0))
    weight1[5] = nd.array([[0, 0, 0], [0, 1, 0], [-10, 0, 0]], gpu(0))
    weight1[6] = nd.array([[0, 0, 0], [0, 1, 0], [0, -10, 0]], gpu(0))
    weight1[7] = nd.array([[0, 0, 0], [0, 1, 0], [0, 0, -10]], gpu(0))

    d0=nd.zeros((rows, cols),ctx[0],dtype='float32')
    dd=nd.zeros((rows, cols),ctx[0],dtype='float32')
    d_flat=nd.zeros((rows, cols),ctx[0],dtype='float32')
    flat=nd.zeros((rows, cols),ctx[0],dtype='float32')
    dep=nd.zeros((rows, cols),ctx[0],dtype='float32')
    high=nd.zeros((rows, cols),ctx[0],dtype='float32')
    fd=nd.zeros((rows, cols),ctx[0],dtype='float32')-999
    d_compact=nd.zeros((rows, cols),ctx[0],dtype='float32')-1

    for i in range(0,8):
        w=weight[i].reshape((1, 1, 3, 3))
        convz[i] = nd.Convolution(data=demmx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off')
        convz[i]=convz[i][0,0,:,:]
        if switch==1 or 3:
            convx[i] = nd.Convolution(data=ingridxmx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off')
            convy[i] = nd.Convolution(data=ingridymx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off')        
            convx[i]=convx[i][0,0,:,:]
            convy[i]=convy[i][0,0,:,:]
        
    if switch==1 or 3:
        for p in range(0,8):#8 facets from N-NE clockwise
            l0=l[p]
            l1=l[p+1]
            d[l0]=d0-999#Nodata value
            dmax=d0-999
            smax=d0-999
            n[l0][0]= convz[l0]*convy[l1]-convz[l1]*convy[l0]#nx
            n[l0][1]= convz[l0]*convx[l1]-convz[l1]*convx[l0]#ny
            n[l0][2]= convy[l0]*convx[l1]-convy[l1]*convx[l0]#nz
            #make boolean mask to determine direction d and slope s
            d[l0]=nd.where(condition=((n[l0][0]==0)*(n[l0][1]>=0)),x=d0,y=d[l0])

            d[l0]=nd.where(condition=((n[l0][0]==0)*(n[l0][1])<0),x=d0+ma.pi,y=d[l0])

            d[l0]=nd.where(condition=(n[l0][0]>0),x=ma.pi/2-nd.arctan(n[l0][1]/n[l0][0]),y=d[l0])

            d[l0]=nd.where(condition=(n[l0][0]<0),x=3*ma.pi/2-nd.arctan(n[l0][1]/n[l0][0]),y=d[l0])


            d[l0]=nd.where(condition=((convz[l0]<=0)*(convz[l1]<=0)),x=dmax,y=d[l0])

            s[l0]=-nd.tan(nd.arccos(n[l0][2]/(nd.sqrt(nd.square(n[l0][0])+nd.square(n[l0][1])+nd.square(n[l0][2])))))#slope of the triangular facet
            s[l0]=nd.where(condition=((convz[l0]<=0)*(convz[l1]<=0)),x=smax,y=s[l0])
            #Modify the scenario when the steepest slope is outside the 45 range of each facet
            dmax=nd.where(condition=((convz[l0]/runlen[l0]>=convz[l1]/runlen[l0])*(convz[l0]>0)),x=d0+ma.pi*l0/4,y=dmax)
            dmax=nd.where(condition=((convz[l0]/runlen[l0]<convz[l1]/runlen[l0])*(convz[l1]>0)),x=d0+ma.pi*(l0+1)/4,y=dmax)

            smax=nd.where(condition=((convz[l0]>=convz[l1])*(convz[l0]>0)),x=convz[l0]/runlen[l0],y=smax)
            smax=nd.where(condition=((convz[l0]<convz[l1])*(convz[l1]>0)),x=convz[l1]/runlen[l1],y=smax)
            d[l0]=nd.where(condition=((d[l0]<ma.pi*l0/4)+(d[l0]>ma.pi*l1/4)),x=dmax,y=d[l0])

            s[l0]=nd.where(condition=((d[l0]<ma.pi*l0/4)+(d[l0]>ma.pi*l1/4)),x=smax,y=s[l0])

            if switch==1:

                #flat and depressions indicator grid    

                flat=(convz[l0]==0)+flat
                dep=(convz[l0]<0)+dep
                high=(convz[l0]>0)+high

        for q in range(0,8):#check if the 45 degree range angles need to be maintaied, otherwise delete (set to NoData)
            l0=l[q]
            l1=l[q+1]
            l2=l[q-1]
            dmax=d0-999
            if q==0:
                dmax=nd.where(condition=(d[0]==d[1]),x=d[0],y=dmax)
                dmax=nd.where(condition=(d[0]==d[7]),x=d[0],y=dmax)
                d[0]=nd.where(condition=((d[0]==ma.pi*l0/4)+(d[0]==ma.pi*l1/4)),x=dmax,y=d[0])
            else:
                dmax=nd.where(condition=(d[l0]==d[l1]),x=d[l0],y=dmax)
                dmax=nd.where(condition=(d[l0]==d[l2]),x=d[l0],y=dmax)
                d[l0]=nd.where(condition=((d[l0]==ma.pi*l0/4)+(d[l0]==ma.pi*l1/4)),x=dmax,y=d[l0])
    #Check if flat or surface depression area. then lable with -1 or -10 respectively

    if switch==1:

        fd=nd.where(condition=(flat==8),x=d0-2,y=fd)#flats

        fd=nd.where(condition=(dep>=1)*(high==0),x=d0-3,y=fd)#high edge

        high_zero=nd.where(condition=(high==0),x=d0+1,y=d0)
    
    
    for j in range (0,8):
        if switch==1 or switch==2:
            d_flat=nd.where(condition=(convz[j]==0),x=d0+direct[j],y=d0)+d_flat
        
        if switch==1:
            flat_near=nd.where(condition=(convz[j]==0),x=d0+5,y=d0)
            dd1=high_zero+flat_near
            w=weight1[j].reshape((1, 1, 3, 3))
            dd1=dd1.reshape((1,1,rows, cols))
            conv_near= nd.Convolution(data=dd1, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off')
            conv_near= conv_near[0,0,:,:]
            dd=nd.where(condition=(conv_near==-5)+(conv_near==-59)+(conv_near==-54)+(conv_near==-4),x=d0+1,y=d0)+dd

        if switch==1 or switch==3:
            d_compact=nd.where(condition=(d[j]==ma.pi*j/4),x=d0+direct_d[j][0],y=d_compact)
            d_compact=nd.where(condition=(d[j]>j*ma.pi/4)*(d[j]<(j+1)*ma.pi/4),x=d0+direct_d[j][1],y=d_compact)

    if switch==1 or switch==3:
        d_compact=nd.where(condition=(dem_fillmx==d0+NoData),x=d0-999,y=d_compact)#NoData        
    
    if switch==1:
        fd=nd.where(condition=(dd>=1)*(high>=1),x=d0-1,y=fd)#low edge
        fd=nd.where(condition=(dep==8),x=d0-10,y=fd)#lowest points in depressions
        return (fd.asnumpy(),d_compact.asnumpy(),d_flat.asnumpy())

    if switch==2:
        return (d_flat.asnumpy())
    if switch==3:
        return (d_compact.asnumpy())