Ejemplo n.º 1
0
def renyi_information(tfr, timestamps=None, freq=None, alpha=3.0):
    """renyi_information

    :param tfr:
    :param timestamps:
    :param freq:
    :param alpha:
    :type tfr:
    :type timestamps:
    :type freq:
    :type alpha:
    :return:
    :rtype:
    """
    if alpha == 1 and tfr.min().min() < 0:
        raise ValueError("Distribution with negative values not allowed.")
    m, n = tfr.shape
    if timestamps is None:
        timestamps = np.arange(n) + 1
    elif np.allclose(timestamps, np.arange(n)):
        timestamps += 1
    if freq is None:
        freq = np.arange(m)
    freq.sort()
    tfr = tfr / integrate_2d(tfr, timestamps, freq)
    if alpha == 1:
        R = -integrate_2d(tfr * np.log2(tfr + np.spacing(1)), timestamps, freq)
    else:
        R = np.log2(integrate_2d(tfr ** alpha, timestamps, freq) + np.spacing(1))
        R = R / (1 - alpha)
    return R
    def calculateEntropy(self,Y, mship):
        """
            calculates the split entropy using Y and mship (logical array) telling which 
            child the examples are being split into...

            Input:
            ---------
                Y: a label array
                mship: (logical array) telling which child the examples are being split into, whether
                        each example is assigned to left split or the right one..
            Returns:
            ---------
                entropy: split entropy of the split
        """

        lexam=Y[mship]
        rexam=Y[np.logical_not(mship)]

        pleft= len(lexam) / float(len(Y))
        pright= 1-pleft

        pl= stats.itemfreq(lexam)[:,1] / float(len(lexam)) + np.spacing(1)
        pr= stats.itemfreq(rexam)[:,1] / float(len(rexam)) + np.spacing(1)

        hl= -np.sum(pl*np.log2(pl)) 
        hr= -np.sum(pr*np.log2(pr)) 

        sentropy = pleft * hl + pright * hr

        return sentropy
Ejemplo n.º 3
0
def klDiv(margDist1, margDist2):
    assert isinstance(margDist1, margDistSCPP) and\
            isinstance(margDist2, margDistSCPP),\
        "margDist1 and margDist2 must be instances of margDistSCPP"
        
    numpy.testing.assert_equal(margDist1.m, 
                               margDist2.m)
    
    kldSum = 0.0
    for idx in xrange(margDist1.m):
        # test that the distributions are over the same bin indices
        # if they are not this calculation is meaninless
        numpy.testing.assert_equal(margDist1.data[idx][1],
                                   margDist2.data[idx][1])
        
        #test that the distributions have the same number of bins
        numpy.testing.assert_equal(len(margDist1.data[idx][0]),
                                   len(margDist2.data[idx][0]))
        
        m1 = margDist1.data[idx][0]
        m1[m1==0] = numpy.spacing(1)
        m2 = margDist2.data[idx][0]
        m2[m2==0] = numpy.spacing(1)
        
             
        kldSum += numpy.sum(margDist1.data[idx][0] * (numpy.log(margDist1.data[idx][0]) 
                                               - numpy.log(margDist2.data[idx][0])))

        
    return kldSum
def add_qualifying_constraint(m,  coefficients, M,  K,  N,  thetaB,  g_flag,  t):
    ''' Add the qualifying constraints to model m.  The qualifying 
        constraint is formed by linearizing the Lagrangian with respect 
        to x about x0. Then linearizing that with respect to theta 
        about thetat.  '''

    qualifying_constraint = []
    x =  [[m.getVarByName("x_%d_%d" % (j,k)) for k in xrange(K)] for j in xrange(M)]

    
    for i in xrange(len(coefficients)):
        #calcuate the qualifying constraint formula
        g_expr = gb.LinExpr()
        g_expr.addConstant(coefficients[i][-1])
        for j in xrange(M*K):
            g_expr.add(x[j/K][j%K]* coefficients[i][j])
        qualifying_constraint.append(g_expr)
        #add the qualifying constraints
        if g_flag[i%K][i/K] != 0:
            ##################### Add constraints: g_expr #########################
            if thetaB[i%K][i/K] == 1:
                m.addConstr(g_expr <= np.spacing(1), name="qc%d_%d_%d" % (t,k,i))
            elif thetaB[i%K][i/K] == 0:
                m.addConstr(g_expr >= -np.spacing(1), name="qc%d_%d_%d" % (t,k,i))
            m.update() 
            

    #return qualifying constraints to calculate lagrange constraint
    return qualifying_constraint
Ejemplo n.º 5
0
def contrast(spec, sr, a):
	""" 
	contrast() - Compute spectral contrast

		Inputs: 
			spec - spectrogram
			sr - sample rate
			a - percentage of frequencies to consider for peak and valley

		Outputs:
			contrasts - array of centroids for each frame

	"""

	nbins, nframes = spec.shape

	fftSize = (nbins - 1) * 2
	freqRes = sr / 2.0 / fftSize

	contrasts = np.zeros(nframes)
	for i in range(nframes):
		sortedFreqs = np.sort(spec[:,i])
		nbinsToLook = np.round(nbins * a).astype("int")

		valley = np.log(np.sum(sortedFreqs[:nbinsToLook]) + np.spacing(1)) / nbinsToLook
		peak = np.log(np.sum(sortedFreqs[nbins-nbinsToLook:nbins]) + np.spacing(1)) / nbinsToLook

		contrasts[i] = peak - valley

	return contrasts
 def generateMuonPlusJet(self,nParticles=1,muonEnergy=10):
     particles=["gamma","pi-","pi+","proton","pi0","e+","e-","kaon0L","kaon+","kaon-"]
     config=open("config.txt","w")
     self.theta=self.maxTheta + np.spacing(1.0)
     while(  self.theta>self.maxTheta or self.theta<0 ):
         self.theta=np.random.normal(self.centralTheta,self.sigmaTheta)
         
     eta=-math.log( math.tan(self.theta/2) )
     phi=random.uniform(0,self.maxPhi)
     config.write( "mu-" + " " + str(muonEnergy) + " " + str(eta) + " " + str(phi) )
     for i in range(0,nParticles):
         config.write("\n")
         part=particles[ random.randint(0,len(particles)-1) ]
         self.theta=self.maxTheta + np.spacing(1.0)
         while(  self.theta>self.maxTheta or self.theta<0 ):
             self.theta=np.random.normal(self.centralTheta,self.sigmaTheta)
         
         eta=-math.log( math.tan(self.theta/2) )
         phi=random.uniform(0,self.maxPhi)
         energy=random.uniform(self.minEnergy,self.maxEnergy)
         if( eta<1.5 ):
             print part + " " + str(energy) + " " + str(eta) + " " + str(phi) 
         phi=random.uniform(0,self.maxPhi)
         config.write( part + " " + str(energy) + " " + str(eta) + " " + str(phi) )
     config.close()
Ejemplo n.º 7
0
    def entropy(self, c1, c2):

        total = c1 + c2
        return -1 * (
            (c1 / total) * numpy.log2((c1 / total) + numpy.spacing(1))
            + (c2 / total) * numpy.log2((c2 / total) + numpy.spacing(1))
        )
Ejemplo n.º 8
0
def colon(r1, inc, r2):
    """
      Matlab's colon operator, althought it doesn't although inc is required

    """

    s = np.sign(inc)

    if s == 0:
        return_value = np.zeros(1)
    elif s == 1:
        n = ((r2 - r1) + 2 * np.spacing(r2 - r1)) // inc
        return_value = np.linspace(r1, r1 + inc * n, n + 1)
    else:  # s == -1:
        # NOTE: I think this is slightly off as we start on the wrong end
        # r1 should be exact, not r2
        n = ((r1 - r2) + 2 * np.spacing(r1 - r2)) // np.abs(inc)
        temp = np.linspace(r2, r2 + np.abs(inc) * n, n + 1)
        return_value = temp[::-1]

    # If the start and steps are whole numbers, we should cast as int
    if(np.equal(np.mod(r1, 1), 0) and
       np.equal(np.mod(s, 1), 0) and
       np.equal(np.mod(r2, 1), 0)):
        return return_value.astype(int)
    else:
        return return_value
Ejemplo n.º 9
0
    def call(self, inputs, mask=None):
        if not isinstance(inputs, list) or len(inputs) <= 1:
            raise TypeError('SpkLifeLongMemory must be called on a list of tensors '
                            '(at least 2). Got: ' + str(inputs))
        # (None(batch), 1), index of speaker
        target_spk_l = inputs[0]
        target_spk_l = K.reshape(target_spk_l, (target_spk_l.shape[0], ))
        if K.dtype(target_spk_l) != 'int32':
            target_spk_l = K.cast(target_spk_l, 'int32')
        # (None(batch), embed_dim)
        spk_vector_l = inputs[1]
        # Start to update life-long memory based on the learned speech vector
        # First do normalization
        spk_vector_eps = K.switch(K.equal(spk_vector_l, 0.), np.spacing(1), spk_vector_l)  # avoid zero
        spk_vector_eps = K.sqrt(K.sum(spk_vector_eps**2, axis=1))
        spk_vector_eps = spk_vector_eps.dimshuffle((0, 'x'))
        spk_vector = T.true_div(spk_vector_l, K.repeat_elements(spk_vector_eps, self.vec_dim, axis=1))
        # Store speech vector into life-long memory according to the speaker identity.
        life_long_mem = T.inc_subtensor(self.life_long_mem[target_spk_l, :], spk_vector)
        # Normalization for memory
        life_long_mem_eps = K.switch(K.equal(life_long_mem, 0.), np.spacing(1), life_long_mem)  # avoid 0
        life_long_mem_eps = K.sqrt(K.sum(life_long_mem_eps**2, axis=1))
        life_long_mem_eps = life_long_mem_eps.dimshuffle((0, 'x'))
        life_long_mem = T.true_div(life_long_mem, K.repeat_elements(life_long_mem_eps, self.vec_dim, axis=1))

        # (None(batch), spk_size, embed_dim)
        return life_long_mem
def solve_master(tree, num_node, Current_node, g_flag, thetaB_list, SUBD,  coefficients, xBar, thetaOpt, lamOpt, muOpt, y,  cons, iteration,  pool=None):
    '''we solve the relaxed master problems based on thetaB_list, then select the infimum of all minimum values.
       Parameters: About the tree : tree, Current_node
                   About the subproblem: SUBD, xBar, thetaOpt, lamOpt, muOpt, y
                   About the boundary: theta_L, theta_U''' 
                   
    (M, N) = np.shape(y)
    K = np.shape(xBar[-1])[0]
    
    x_stor = None
    Q_stor = np.inf
    next_node = -1
    
    #store all the MLBD
    MLBD_stor = [] 

    #store all the MLBD
    if pool == None:
        tree.nodes[Current_node].set_parameters_qualifying_constraint(lamOpt,  thetaOpt, muOpt,  xBar, SUBD,  g_flag,  coefficients)
        #check whether the coefficients are already stored into the parents or not.
        
        print ('\n%d master problems are solving...'  %len(thetaB_list))

        for index in xrange(len(thetaB_list)):
            thetaB = thetaB_list[index].copy()
            status, objVal, xOpt,  thetaB, lagrangian_coefficient= solve_master_s(tree, Current_node, coefficients, thetaOpt, xBar, lamOpt, muOpt, thetaB.copy(), y, g_flag, cons)
            #print objVal, xOpt
            
            if status == 2 and objVal < SUBD - np.spacing(1):
                node = tree.add_node(num_node, 0, 1, Current_node)
                node.set_parameters_thetaB(thetaB,  xOpt, objVal, lagrangian_coefficient)
                MLBD_stor.append(objVal)
                if objVal < Q_stor:
                    Q_stor = objVal
                    next_node = num_node
                    x_stor = xOpt
                num_node = num_node + 1

    else:
        tree.nodes[Current_node].set_parameters_qualifying_constraint(lamOpt,  thetaOpt, muOpt,  xBar, SUBD,  g_flag,  coefficients)
        len_thetaB = len(thetaB_list)
        print ('\n%d master problems are solving...'  %len_thetaB)
        results = [pool.apply_async(solve_master_s,  args = (tree, Current_node, coefficients, thetaOpt, xBar, lamOpt, muOpt, thetaB.copy(), y, g_flag, cons)) for thetaB in thetaB_list]

        #put all the result into the tree.
        for p in results:
            #result = [status, objVal, xOpt, thetaB]
            result = p.get() 
            if result[0] == 2 and result[1] < SUBD - np.spacing(1):
                node = tree.add_node(num_node,  0,  1,  Current_node)
                node.set_parameters_thetaB(result[3],  result[2],  result[1], result[4])
                #node.set_parameter(lamOpt,  thetaOpt, result[3],  muOpt,  xBar,  result[2],  SUBD,  result[1], g_flag,  coefficients)
                MLBD_stor.append(result[1])
                if result[1] < Q_stor:
                    Q_stor = result[1]
                    next_node = num_node
                    x_stor =  result[2]
                num_node += 1

    return x_stor, Q_stor, next_node, num_node, MLBD_stor
def _frank(M, N, alpha):
    if(N<2):
        raise ValueError('Dimensionality Argument [N] must be an integer >= 2')
    elif(N==2):        
        u1 = uniform.rvs(size=M)
        p = uniform.rvs(size=M)
        if abs(alpha) > math.log(sys.float_info.max):
            u2 = (u1 < 0).astype(int) + np.sign(alpha)*u1  # u1 or 1-u1
        elif abs(alpha) > math.sqrt(np.spacing(1)):
            u2 = -1*np.log((np.exp(-alpha*u1)*(1-p)/p + np.exp(-alpha))/(1 + np.exp(-alpha*u1)*(1-p)/p))/alpha
        else:
            u2 = p
        
        U = np.column_stack((u1,u2))
    else:
        # Algorithm 1 described in both the SAS Copula Procedure, as well as the
        # paper: "High Dimensional Archimedean Copula Generation Algorithm"
        if(alpha<=0):
            raise ValueError('For N>=3, alpha >0 in Frank Copula')
            
        U = np.empty((M,N))
        for ii in range(0,M):
            p = -1.0*np.expm1(-1*alpha)
            if(p==1):
                # boundary case protection
                p = 1 - np.spacing(1)
            v = logser.rvs(p, size=1)
            
            # sample N independent uniform random variables
            x_i = uniform.rvs(size=N)
            t = -1*np.log(x_i)/v
            U[ii,:] = -1.0*np.log1p( np.exp(-t)*np.expm1(-1.0*alpha))/alpha
            
    return U
Ejemplo n.º 12
0
    def test_half_fpe(self):
        oldsettings = np.seterr(all="raise")
        try:
            sx16 = np.array((1e-4,), dtype=float16)
            bx16 = np.array((1e4,), dtype=float16)
            sy16 = float16(1e-4)
            by16 = float16(1e4)

            # Underflow errors
            assert_raises_fpe("underflow", lambda a, b: a * b, sx16, sx16)
            assert_raises_fpe("underflow", lambda a, b: a * b, sx16, sy16)
            assert_raises_fpe("underflow", lambda a, b: a * b, sy16, sx16)
            assert_raises_fpe("underflow", lambda a, b: a * b, sy16, sy16)
            assert_raises_fpe("underflow", lambda a, b: a / b, sx16, bx16)
            assert_raises_fpe("underflow", lambda a, b: a / b, sx16, by16)
            assert_raises_fpe("underflow", lambda a, b: a / b, sy16, bx16)
            assert_raises_fpe("underflow", lambda a, b: a / b, sy16, by16)
            assert_raises_fpe("underflow", lambda a, b: a / b, float16(2.0 ** -14), float16(2 ** 11))
            assert_raises_fpe("underflow", lambda a, b: a / b, float16(-2.0 ** -14), float16(2 ** 11))
            assert_raises_fpe("underflow", lambda a, b: a / b, float16(2.0 ** -14 + 2 ** -24), float16(2))
            assert_raises_fpe("underflow", lambda a, b: a / b, float16(-2.0 ** -14 - 2 ** -24), float16(2))
            assert_raises_fpe("underflow", lambda a, b: a / b, float16(2.0 ** -14 + 2 ** -23), float16(4))

            # Overflow errors
            assert_raises_fpe("overflow", lambda a, b: a * b, bx16, bx16)
            assert_raises_fpe("overflow", lambda a, b: a * b, bx16, by16)
            assert_raises_fpe("overflow", lambda a, b: a * b, by16, bx16)
            assert_raises_fpe("overflow", lambda a, b: a * b, by16, by16)
            assert_raises_fpe("overflow", lambda a, b: a / b, bx16, sx16)
            assert_raises_fpe("overflow", lambda a, b: a / b, bx16, sy16)
            assert_raises_fpe("overflow", lambda a, b: a / b, by16, sx16)
            assert_raises_fpe("overflow", lambda a, b: a / b, by16, sy16)
            assert_raises_fpe("overflow", lambda a, b: a + b, float16(65504), float16(17))
            assert_raises_fpe("overflow", lambda a, b: a - b, float16(-65504), float16(17))
            assert_raises_fpe("overflow", np.nextafter, float16(65504), float16(np.inf))
            assert_raises_fpe("overflow", np.nextafter, float16(-65504), float16(-np.inf))
            assert_raises_fpe("overflow", np.spacing, float16(65504))

            # Invalid value errors
            assert_raises_fpe("invalid", np.divide, float16(np.inf), float16(np.inf))
            assert_raises_fpe("invalid", np.spacing, float16(np.inf))
            assert_raises_fpe("invalid", np.spacing, float16(np.nan))
            assert_raises_fpe("invalid", np.nextafter, float16(np.inf), float16(0))
            assert_raises_fpe("invalid", np.nextafter, float16(-np.inf), float16(0))
            assert_raises_fpe("invalid", np.nextafter, float16(0), float16(np.nan))

            # These should not raise
            float16(65472) + float16(32)
            float16(2 ** -13) / float16(2)
            float16(2 ** -14) / float16(2 ** 10)
            np.spacing(float16(-65504))
            np.nextafter(float16(65504), float16(-np.inf))
            np.nextafter(float16(-65504), float16(np.inf))
            float16(2 ** -14) / float16(2 ** 10)
            float16(-2 ** -14) / float16(2 ** 10)
            float16(2 ** -14 + 2 ** -23) / float16(2)
            float16(-2 ** -14 - 2 ** -23) / float16(2)
        finally:
            np.seterr(**oldsettings)
def interior_point_using_distance(coefficients, nCuts,  dim,  marker, weight, index = -1):
    ''' Get the interior point from the certain region described by marker. The returned point should be far from all the hyperplanes. 
    coefficients: the coefficients of hyperplanes with the same order. For example, (a1, a2, a3, c) in the hyperplane (a1 * x1 + a2 * x2 + a3 * x3 + c = 0)
    nCuts: the number of the hyerplanes.
    Dim: the dimension of the space.
    marker: the sign of the hyperplane.
    weight: calcualte the norm 2 of the coefficients except the constant of all hyperplanes  
    index: the hyperplane we want to trim. Usually index = -1, which means no hyperplane will be trimed.
    
    Here, we add the different weights for different hyperplanes when we calculate the distance.

    maximize z
     subject to  A*x + b + weight*z <= np.spacing(1), for all sign >=0
                       -1*(A*x + b) + weight*z <= np.spacing(1), for all sign <= 0
    '''
    
    #Create a new model
    m = gb.Model('Interior_point')
    
    #Set parameters
    m.setParam('OutputFlag',  False)
    
    x = [0 for i in xrange(dim)]
    for i in xrange(dim):
        x[i] = m.addVar(lb = -1e7,  ub = 1e7,  vtype=gb.GRB.CONTINUOUS,  name = 'x_%d'%(i))
    
    obj = m.addVar(lb = 0.0,  vtype = gb.GRB.CONTINUOUS,  name = 'objective')
    m.update()
    
    for i in xrange(nCuts):
        #hyperplane index is trimed, so there is no constraint for index hyperplane.
        if index != i:
            g_expr = gb.LinExpr()
            g_expr.addConstant(coefficients[i][-1])
            for j in xrange(dim):
                g_expr.add(x[j] * coefficients[i][j])
            if marker[i] == 0:
                m.addConstr(-1 * g_expr + weight[i]*obj <= np.spacing(0),  name = 'qc_%d' %(i))
            elif marker[i] == 1:
                m.addConstr(g_expr + weight[i]*obj <= np.spacing(0),  name = 'qc_%d' %(i))
    m.update()
    
    #Create the objective : maximize obj
    m.setObjective(obj,  gb.GRB.MAXIMIZE)
    m.update()
    
    #Optimize the test problem.
    try:
        m.optimize()
    except gb.GurobiError as e:
        print e.message

    if m.Status == gb.GRB.OPTIMAL:
        xOpt = np.empty(dim)
        for i in xrange(dim):
            xOpt[i] = x[i].x
        return m.Status,  xOpt,  obj.x
    else:
        return m.Status,  np.nan,  np.nan
def ellipse_axis_length( a ):
    b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
    up = 2*(a*f*f+c*d*d+g*b*b-2*b*d*f-a*c*g)
    down1=(b*b-a*c)*( (c-a)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
    down2=(b*b-a*c)*( (a-c)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
    res1=np.sqrt(up/(np.abs(down1)+np.spacing(1)))
    res2=np.sqrt(up/(np.abs(down2)+np.spacing(1)))
    return np.array([res1, res2])
Ejemplo n.º 15
0
def estimate_parameters(left_stft, right_stft, stft_window_length):
	#List comprehensions are amazing!
	w_range = [i for j in (range(1, stft_window_length/2+1), range(-stft_window_length/2 + 1, 0)) for i in j]
	w = np.array([2*np.pi*i/(stft_window_length)
		for i in w_range
		if i is not 0])
	delay_estimation = -1/w * np.angle((right_stft + np.spacing(1))/(left_stft + np.spacing(1)))
	attenuation_estimation = np.absolute(right_stft/left_stft) - np.absolute(left_stft/right_stft)
	return attenuation_estimation, delay_estimation
Ejemplo n.º 16
0
    def _evenly_spaced_condition_num_helper(self, p_order):
        import numpy as np

        x_vals = np.linspace(-1, 1, p_order + 1)
        leg_mat = self._call_func_under_test(x_vals)
        kappa2 = np.linalg.cond(leg_mat, p=2)
        # This gives the exponent of kappa2.
        base_exponent = np.log2(np.spacing(1.0))
        return int(np.round(np.log2(np.spacing(kappa2)) - base_exponent))
Ejemplo n.º 17
0
def lanczos2(x):
    # f = (sin(pi*x) .* sin(pi*x/2) + eps) ./ ((pi^2 * x.^2 / 2) + eps);
    # f = f .* (abs(x) < 2);
    result = (np.sin(np.pi * x) * np.sin(np.pi * x / 2 + np.spacing(1))) / (
        (np.power(np.pi, 2) * np.power(x, 2) / 2) + np.spacing(1)
    )
    print (np.abs(result) < 2)
    result = result * (np.abs(result) < 2)
    return result
Ejemplo n.º 18
0
def t_calc_information(p_x_given_t, PYgivenTs, PXs, PYs):
    """Calculate the MI - I(X;T) and I(Y;T)"""
    Hx = np.nansum(-np.dot(PXs, np.log2(PXs)))
    Hxt = - np.nansum((np.dot(np.multiply(p_x_given_t, np.log2(p_x_given_t)), PXs)))
    Hyt = - np.nansum(np.dot(PYgivenTs*np.log2(PYgivenTs+np.spacing(1)), PTs))
    Hy = np.nansum(-PYs * np.log2(PYs+np.spacing(1)))
    IYT = Hy - Hyt
    ITX = Hx - Hxt
    return ITX, IYT
Ejemplo n.º 19
0
def checkResult(Lbar,eigvec,eigval,k):
	"""
	"input
	"matrix Lbar and k eig values and k eig vectors
	"print norm(Lbar*eigvec[:,i]-lamda[i]*eigvec[:,i])
	"""
	check=[np.dot(Lbar,eigvec[:,i])-eigval[i]*eigvec[:,i] for i in range(0,k)]
	length=[np.linalg.norm(e) for e in check]/np.spacing(1)
	print("Lbar*v-lamda*v are %s*%s" % (length,np.spacing(1)))
def test_segmentation_utils():
    ctx = mx.context.current_context()
    import os
    if not os.path.isdir(os.path.expanduser('~/.mxnet/datasets/voc')):
        return

    transform_fn = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([.485, .456, .406], [.229, .224, .225])
    ])
    # get the dataset
    # TODO FIXME: change it to ADE20K dataset and pretrained model
    dataset = ADE20KSegmentation(split='val')
    # load pretrained net
    net = gluoncv.model_zoo.get_model('fcn_resnet50_ade', pretrained=True, ctx=ctx)
    # count for pixAcc and mIoU
    total_inter, total_union, total_correct, total_label = 0, 0, 0, 0
    np_inter, np_union, np_correct, np_label = 0, 0, 0, 0
    tbar = tqdm(range(10))
    for i in tbar:
        img, mask = dataset[i]
        # prepare data and make prediction
        img = transform_fn(img)
        img = img.expand_dims(0).as_in_context(ctx)
        mask = mask.expand_dims(0)
        pred = net.evaluate(img).as_in_context(mx.cpu(0))
        # gcv prediction
        correct1, labeled1 = batch_pix_accuracy(pred, mask)
        inter1, union1 = batch_intersection_union(pred, mask, dataset.num_class)
        total_correct += correct1
        total_label += labeled1
        total_inter += inter1
        total_union += union1
        pixAcc = 1.0 * total_correct / (np.spacing(1) + total_label)
        IoU = 1.0 * total_inter / (np.spacing(1) + total_union)
        mIoU = IoU.mean()

        # np predicition
        pred2 = np.argmax(pred.asnumpy().astype('int64'), 1) + 1
        mask2 = mask.squeeze().asnumpy().astype('int64') + 1
        _, correct2, labeled2 = pixelAccuracy(pred2, mask2)
        inter2, union2 = intersectionAndUnion(pred2, mask2, dataset.num_class)
        np_correct += correct2
        np_label += labeled2
        np_inter += inter2
        np_union += union2
        np_pixAcc = 1.0 * np_correct / (np.spacing(1) + np_label)
        np_IoU = 1.0 * np_inter / (np.spacing(1) + np_union)
        np_mIoU = np_IoU.mean()
        tbar.set_description('pixAcc: %.3f, np_pixAcc: %.3f, mIoU: %.3f, np_mIoU: %.3f'%\
            (pixAcc, np_pixAcc, mIoU, np_mIoU))

    np.testing.assert_allclose(total_inter, np_inter)
    np.testing.assert_allclose(total_union, np_union)
    np.testing.assert_allclose(total_correct, np_correct)
    np.testing.assert_allclose(total_label, np_label)
Ejemplo n.º 21
0
def My_Angle(u,v):
    du = sqrt(sum(u**2))
    dv = sqrt(sum(v**2))
    du = max(du,spacing(1))
    dv = max(dv,spacing(1))

    x = sum(u*v) / (du*dv)
    x = clamp(x, -1.0, 1.0)
    angle = acos(x)
    return angle
Ejemplo n.º 22
0
def test_spacing():
    for t in [np.float32, np.float64, np.longdouble]:
        one = t(1)
        eps = np.finfo(t).eps
        nan = t(np.nan)
        inf = t(np.inf)
        assert np.spacing(one) == eps
        assert np.isnan(np.spacing(nan))
        assert np.isnan(np.spacing(inf))
        assert np.isnan(np.spacing(-inf))
Ejemplo n.º 23
0
def _test_spacing(t):
    one = t(1)
    eps = np.finfo(t).eps
    nan = t(np.nan)
    inf = t(np.inf)
    assert np.spacing(one) == eps
    assert np.isnan(np.spacing(nan))
    assert np.isnan(np.spacing(inf))
    assert np.isnan(np.spacing(-inf))
    assert np.spacing(t(1e30)) != 0
Ejemplo n.º 24
0
def calc_information(probTgivenXs, PYgivenTs, PXs, PYs):
    """Calculate the MI - I(X;T) and I(Y;T)"""
    PTs = np.nansum(probTgivenXs*PXs, axis=1)
    Ht = np.nansum(-np.dot(PTs, np.log2(PTs)))
    Htx = - np.nansum((np.dot(np.multiply(probTgivenXs, np.log2(probTgivenXs)), PXs)))
    Hyt = - np.nansum(np.dot(PYgivenTs*np.log2(PYgivenTs+np.spacing(1)), PTs))
    Hy = np.nansum(-PYs * np.log2(PYs+np.spacing(1)))
    IYT = Hy - Hyt
    ITX = Ht - Htx
    return ITX, IYT
Ejemplo n.º 25
0
def is_converged(fval, prev_fval, thresh=1e-4, warn=False):
    """Check if an objective function has converged.

    Parameters
    ----------
    fval : float
        The current value.
    prev_fval : float
        The previous value.
    thresh : float
        The convergence threshold.
    warn : bool
        Flag indicating whether to warn the user when the
        fval decreases.

    Returns
    -------
    bool :
        Flag indicating whether the objective function has converged or not.

    Notes
    -----
    The test returns true if the slope of the function falls below the threshold; i.e.

    .. math::

        \\frac{|f(t) - f(t-1)|}{\\text{avg}} < \\text{thresh},

    where

    .. math::

         \\text{avg} = \\frac{|f(t)| + |f(t+1)|}{2}

    .. note::
        Ported from Matlab:

        | Project: `Probabilistic Modeling Toolkit for Matlab/Octave <https://github.com/probml/pmtk3>`_.
        | Copyright (2010) Kevin Murphy and Matt Dunham
        | License: `MIT <https://github.com/probml/pmtk3/blob/5fefd068a2e84ae508684d3e4750bd72a4164ba0/license.txt>`_

    """
    converged = False
    delta_fval = abs(fval - prev_fval)
    # noinspection PyTypeChecker
    avg_fval = np.true_divide(abs(fval) + abs(prev_fval) + np.spacing(1), 2)

    if float(delta_fval) / float(avg_fval) < thresh:
        converged = True

    if warn and (fval - prev_fval) < -2 * np.spacing(1):
        print("is_converged: fval decrease, objective decreased!")

    return converged
Ejemplo n.º 26
0
    def _chebyshev_conditioning_helper(self, num_nodes):
        import numpy as np

        theta = np.pi * np.arange(2 * num_nodes - 1, 0, -2,
                                  dtype=np.float64) / (2 * num_nodes)
        x_vals = np.cos(theta)
        leg_mat = self._call_func_under_test(x_vals)
        kappa2 = np.linalg.cond(leg_mat, p=2)
        # This gives the exponent of kappa2.
        base_exponent = np.log2(np.spacing(1.0))
        return int(np.round(np.log2(np.spacing(kappa2)) - base_exponent))
Ejemplo n.º 27
0
def _test_spacing(t):
    one = t(1)
    eps = np.finfo(t).eps
    nan = t(np.nan)
    inf = t(np.inf)
    with np.errstate(invalid='ignore'):
        assert_(np.spacing(one) == eps)
        assert_(np.isnan(np.spacing(nan)))
        assert_(np.isnan(np.spacing(inf)))
        assert_(np.isnan(np.spacing(-inf)))
        assert_(np.spacing(t(1e30)) != 0)
Ejemplo n.º 28
0
def forward_problem_hessian(theta, N):
    """ Gets the etas for given thetas. Here a costum-made iterative solver is
    used.

    :param numpy.ndarray theta:
        (d,)-dimensional array containing all thetas
    :param int N:
        Number of cells

    :returns:
        (d,) numpy.ndarray with all etas.
    """
    # Initialize eta vector
    eta = numpy.empty(theta.shape)
    eta_max = 0.5*numpy.ones(N)
    # Extract first order thetas
    theta1 = theta[:N]
    # Get indices
    triu_idx = numpy.triu_indices(N, k=1)
    diag_idx = numpy.diag_indices(N)
    # Write second order thetas into matrix
    theta2 = numpy.zeros([N, N])
    theta2[triu_idx] = theta[N:]
    theta2 += theta2.T
    conv = numpy.inf
    # Solve self-consistent equations and calculate approximation of
    # fisher matrix
    iter_num = 0
    while conv > 1e-4 and iter_num < 500:
        deta = self_consistent_eq(eta_max, theta1=theta1, theta2=theta2,
                                  expansion='TAP')
        Hinv = self_consistent_eq_Hinv(eta_max, theta1=theta1, theta2=theta2,
                                       expansion='TAP')
        eta_max -= .1*numpy.dot(Hinv, deta)
        conv = numpy.amax(numpy.absolute(deta))
        iter_num += 1
        eta_max[eta_max <= 0.] = numpy.spacing(1)
        eta_max[eta_max >= 1.] = 1. - numpy.spacing(1)
        if iter_num == 500:
            raise Exception('Self consistent equations could not be solved!')

    G_inv = - theta2 - theta2**2*numpy.outer(0.5 - eta_max[:N],
                                             0.5 - eta_max[:N])
    G_inv[diag_idx] = 1./eta_max + 1./(1.-eta_max) + .5*numpy.dot(theta2**2,
                                                                  (eta_max -
                                                                   eta_max**2))
    G = numpy.linalg.inv(G_inv)
    # Compute second order eta
    eta2 = G + numpy.outer(eta_max[:N], eta_max[:N])
    eta[N:] = eta2[triu_idx]
    eta[:N] = eta_max
    eta[eta < 0.] = numpy.spacing(1)
    eta[eta > 1.] = 1. - numpy.spacing(1)
    return eta
Ejemplo n.º 29
0
    def get(self):
        """Gets the current evaluation result.

        Returns
        -------
        metrics : tuple of float
            pixAcc and mIoU
        """
        pixAcc = 1.0 * self.total_correct / (np.spacing(1) + self.total_label)
        IoU = 1.0 * self.total_inter / (np.spacing(1) + self.total_union)
        mIoU = IoU.mean()
        return pixAcc, mIoU
def allr( w, lPx_all, cls, y, lamb ):
    lPx_pos = lPx_all[cls,:,y == (cls+1)].T
    lPx_neg = lPx_all[cls,:,y != (cls+1)].T
    
    P = ( numpy.log( numpy.spacing(1) + (numpy.exp( lPx_pos )*w[None,:,None] ).sum(1)  / \
    ( ( numpy.exp( lPx_all[:,:,y == (cls+1)] )*w[None,:,None] ).sum(1)).sum(0) )).sum() / (y == (cls+1)).sum(0)
    
    N = ( numpy.log( numpy.spacing(1) + (numpy.exp( lPx_neg )*w[None,:,None] ).sum(1)  / \
    ( ( numpy.exp( lPx_all[:,:,y != (cls+1)] )*w[None,:,None] ).sum(1)).sum(0) )).sum() / (y != (cls+1)).sum(0)
    return -( P-N + lamb*((w**2).sum())  )
    
    
    
Ejemplo n.º 31
0
            poly.append(np.sqrt((2*len(b) - 1) / 2) * igral)

        result.append(np.array(poly))
    return result


def calc_V(x, n):
    V = [np.ones(x.shape), x.copy()]
    for i in range(2, n):
        V.append((2*i-1) / i * x * V[-1] - (i-1) / i * V[-2])
    for i in range(n):
        V[i] *= np.sqrt(i + 0.5)
    return np.array(V).T


eps = np.spacing(1)

# the nodes and Newton polynomials
ns = (5, 9, 17, 33)
xi = [-np.cos(np.linspace(0, np.pi, n)) for n in ns]

# Make `xi` perfectly anti-symmetric, important for splitting the intervals
xi = [(row - row[::-1]) / 2 for row in xi]

# Compute the Vandermonde-like matrix and its inverse.
V = [calc_V(x, n) for x, n in zip(xi, ns)]
V_inv = list(map(scipy.linalg.inv, V))
Vcond = [scipy.linalg.norm(a, 2) * scipy.linalg.norm(b, 2) for a, b in zip(V, V_inv)]

# Compute the shift matrices.
T_left, T_right = [V_inv[3] @ calc_V((xi[3] + a) / 2, ns[3]) for a in [-1, 1]]
Ejemplo n.º 32
0
def corner_subpix(image, corners, window_size=11, alpha=0.99):
    """Determine subpixel position of corners.

    A statistical test decides whether the corner is defined as the
    intersection of two edges or a single peak. Depending on the classification
    result, the subpixel corner location is determined based on the local
    covariance of the grey-values. If the significance level for either
    statistical test is not sufficient, the corner cannot be classified, and
    the output subpixel position is set to NaN.

    Parameters
    ----------
    image : ndarray
        Input image.
    corners : (N, 2) ndarray
        Corner coordinates `(row, col)`.
    window_size : int, optional
        Search window size for subpixel estimation.
    alpha : float, optional
        Significance level for corner classification.

    Returns
    -------
    positions : (N, 2) ndarray
        Subpixel corner positions. NaN for "not classified" corners.

    References
    ----------
    .. [1] Förstner, W., & Gülch, E. (1987, June). A fast operator for detection and
           precise location of distinct points, corners and centres of circular
           features. In Proc. ISPRS intercommission conference on fast processing of
           photogrammetric data (pp. 281-305).
           https://cseweb.ucsd.edu/classes/sp02/cse252/foerstner/foerstner.pdf
    .. [2] https://en.wikipedia.org/wiki/Corner_detection

    Examples
    --------
    >>> from skimage.feature import corner_harris, corner_peaks, corner_subpix
    >>> img = np.zeros((10, 10))
    >>> img[:5, :5] = 1
    >>> img[5:, 5:] = 1
    >>> img.astype(int)
    array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
           [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
           [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
           [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
           [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
    >>> coords = corner_peaks(corner_harris(img), min_distance=2,
    ...                       threshold_rel=0)
    >>> coords_subpix = corner_subpix(img, coords, window_size=7)
    >>> coords_subpix
    array([[4.5, 4.5]])

    """

    # window extent in one direction
    wext = (window_size - 1) // 2

    image = np.pad(image, pad_width=wext, mode='constant', constant_values=0)

    # add pad width, make sure to not modify the input values in-place
    corners = safe_as_int(corners + wext)

    # normal equation arrays
    N_dot = np.zeros((2, 2), dtype=np.double)
    N_edge = np.zeros((2, 2), dtype=np.double)
    b_dot = np.zeros((2, ), dtype=np.double)
    b_edge = np.zeros((2, ), dtype=np.double)

    # critical statistical test values
    redundancy = window_size**2 - 2
    t_crit_dot = stats.f.isf(1 - alpha, redundancy, redundancy)
    t_crit_edge = stats.f.isf(alpha, redundancy, redundancy)

    # coordinates of pixels within window
    y, x = np.mgrid[-wext:wext + 1, -wext:wext + 1]

    corners_subpix = np.zeros_like(corners, dtype=np.double)

    for i, (y0, x0) in enumerate(corners):

        # crop window around corner + border for sobel operator
        miny = y0 - wext - 1
        maxy = y0 + wext + 2
        minx = x0 - wext - 1
        maxx = x0 + wext + 2
        window = image[miny:maxy, minx:maxx]

        winx, winy = _compute_derivatives(window, mode='constant', cval=0)

        # compute gradient suares and remove border
        winx_winx = (winx * winx)[1:-1, 1:-1]
        winx_winy = (winx * winy)[1:-1, 1:-1]
        winy_winy = (winy * winy)[1:-1, 1:-1]

        # sum of squared differences (mean instead of gaussian filter)
        Axx = np.sum(winx_winx)
        Axy = np.sum(winx_winy)
        Ayy = np.sum(winy_winy)

        # sum of squared differences weighted with coordinates
        # (mean instead of gaussian filter)
        bxx_x = np.sum(winx_winx * x)
        bxx_y = np.sum(winx_winx * y)
        bxy_x = np.sum(winx_winy * x)
        bxy_y = np.sum(winx_winy * y)
        byy_x = np.sum(winy_winy * x)
        byy_y = np.sum(winy_winy * y)

        # normal equations for subpixel position
        N_dot[0, 0] = Axx
        N_dot[0, 1] = N_dot[1, 0] = -Axy
        N_dot[1, 1] = Ayy

        N_edge[0, 0] = Ayy
        N_edge[0, 1] = N_edge[1, 0] = Axy
        N_edge[1, 1] = Axx

        b_dot[:] = bxx_y - bxy_x, byy_x - bxy_y
        b_edge[:] = byy_y + bxy_x, bxx_x + bxy_y

        # estimated positions
        try:
            est_dot = np.linalg.solve(N_dot, b_dot)
            est_edge = np.linalg.solve(N_edge, b_edge)
        except np.linalg.LinAlgError:
            # if image is constant the system is singular
            corners_subpix[i, :] = np.nan, np.nan
            continue

        # residuals
        ry_dot = y - est_dot[0]
        rx_dot = x - est_dot[1]
        ry_edge = y - est_edge[0]
        rx_edge = x - est_edge[1]
        # squared residuals
        rxx_dot = rx_dot * rx_dot
        rxy_dot = rx_dot * ry_dot
        ryy_dot = ry_dot * ry_dot
        rxx_edge = rx_edge * rx_edge
        rxy_edge = rx_edge * ry_edge
        ryy_edge = ry_edge * ry_edge

        # determine corner class (dot or edge)
        # variance for different models
        var_dot = np.sum(winx_winx * ryy_dot - 2 * winx_winy * rxy_dot +
                         winy_winy * rxx_dot)
        var_edge = np.sum(winy_winy * ryy_edge + 2 * winx_winy * rxy_edge +
                          winx_winx * rxx_edge)

        # test value (F-distributed)
        if var_dot < np.spacing(1) and var_edge < np.spacing(1):
            t = np.nan
        elif var_dot == 0:
            t = np.inf
        else:
            t = var_edge / var_dot

        # 1 for edge, -1 for dot, 0 for "not classified"
        corner_class = int(t < t_crit_edge) - int(t > t_crit_dot)

        if corner_class == -1:
            corners_subpix[i, :] = y0 + est_dot[0], x0 + est_dot[1]
        elif corner_class == 0:
            corners_subpix[i, :] = np.nan, np.nan
        elif corner_class == 1:
            corners_subpix[i, :] = y0 + est_edge[0], x0 + est_edge[1]

    # subtract pad width
    corners_subpix -= wext

    return corners_subpix
Ejemplo n.º 33
0
def test_ufunc_spacing_c(A: dace.complex64[10]):
    return np.spacing(A)
Ejemplo n.º 34
0
def decomposition_criteria(Se, Sr, noise):
    r"""
    Computes the SDR of each couple reference/estimate sources.

    Inputs
    ------
    Se: numpy array
        estimateof column signals.
    Sr: numpy array
        reference  of column signals.
    noise: numpy array
        noise matrix cotaminating the data.

    Outputs
    ------
    criteria: dict
        value of each of the criteria.
    decomposition: dict
        decomposition of the estimated sources under target, interference,
        noise and artifacts.
    """
    # compute projections
    Sr = Sr / core.tools.dim_norm(Sr, 0)
    pS = Sr.dot(np.linalg.lstsq(Sr, Se)[0])
    SN = np.hstack([Sr, noise])
    #pSN = SN.dot(np.linalg.lstsq(SN, Se)[0])  # this crashes on MAC
    pSN = SN.dot(linalg.lstsq(SN, Se)[0])
    eps = np.spacing(1)
    # compute decompositions
    decomposition = {}
    # targets
    decomposition['target'] = np.sum(Se * Sr, 0) * Sr  # Sr is normalized
    # interferences
    decomposition['interferences'] = pS - decomposition['target']
    # noise
    decomposition['noise'] = pSN - pS
    # artifacts
    decomposition['artifacts'] = Se - pSN
    # compute criteria
    criteria = {}
    # SDR: source to distortion ratio
    num = decomposition['target']
    den = decomposition['interferences'] +\
        (decomposition['noise'] + decomposition['artifacts'])
    norm_num_2 = np.sum(num * num, 0)
    norm_den_2 = np.sum(den * den, 0)
    criteria['SDR'] = np.mean(
        10 *
        np.log10(np.maximum(norm_num_2, eps) / np.maximum(norm_den_2, eps)))
    criteria['SDR median'] = np.median(
        10 *
        np.log10(np.maximum(norm_num_2, eps) / np.maximum(norm_den_2, eps)))
    # SIR: source to interferences ratio
    num = decomposition['target']
    den = decomposition['interferences']
    norm_num_2 = sum(num * num, 0)
    norm_den_2 = sum(den * den, 0)
    criteria['SIR'] = np.mean(
        10 *
        np.log10(np.maximum(norm_num_2, eps) / np.maximum(norm_den_2, eps)))
    # SNR: source to noise ratio
    if np.max(np.abs(noise)) > 0:  # only if there is noise
        num = decomposition['target'] + decomposition['interferences']
        den = decomposition['noise']
        norm_num_2 = sum(num * num, 0)
        norm_den_2 = sum(den * den, 0)
        criteria['SNR'] = np.mean(10 * np.log10(
            np.maximum(norm_num_2, eps) / np.maximum(norm_den_2, eps)))
    else:
        criteria['SNR'] = np.inf
    # SAR: sources to artifacts ratio
    if (noise.shape[1] + Sr.shape[1] < Sr.shape[0]):
        # if noise + sources form a basis, there is no "artifacts"
        num = decomposition['target'] +\
            (decomposition['interferences'] + decomposition['noise'])
        den = decomposition['artifacts']
        norm_num_2 = sum(num * num, 0)
        norm_den_2 = sum(den * den, 0)
        criteria['SAR'] = np.mean(10 * np.log10(
            np.maximum(norm_num_2, eps) / np.maximum(norm_den_2, eps)))
    else:
        criteria['SAR'] = np.inf
    return (criteria, decomposition)
Ejemplo n.º 35
0
    def __init__(self, dataset='ansim', ov=3, split=1, nfft=1024, db=30, wav_extra_name='', desc_extra_name=''):

        # TODO: Change the path according to your machine.
        # TODO: It should point to a folder which consists of sub-folders for audio and metada
        if dataset == 'ansim':
            self._base_folder = 'data/' #os.path.join('/scratch/asignal/sharath', 'doa_data/')
        elif dataset == 'resim':
            self._base_folder = os.path.join('/proj/asignal/TUT_SELD/', 'doa_data_echoic/')
        elif dataset == 'cansim':
            self._base_folder = os.path.join('/proj/asignal/TUT_SELD/', 'doa_circdata/')
        elif dataset == 'cresim':
            self._base_folder = os.path.join('/proj/asignal/TUT_SELD/', 'doa_circdata_echoic/')
        elif dataset == 'real':
            self._base_folder = os.path.join('/proj/asignal/TUT_SELD/', 'tut_seld_data/')
        elif dataset == 'mansim':
            self._base_folder = os.path.join('/proj/asignal/TUT_SELD/', 'moving_sound_events_foa/')
        elif dataset == 'mreal':
            self._base_folder = os.path.join('/proj/asignal/TUT_SELD/', 'tut_seld_movingdata_foa/')

        # Input directories
        self._aud_dir = os.path.join(self._base_folder, 'wav_ov{}_split{}_{}db{}'.format(ov, split, db, wav_extra_name))
        self._desc_dir = os.path.join(self._base_folder, 'desc_ov{}_split{}{}'.format(ov, split, desc_extra_name))

        # Output directories
        self._label_dir = None
        self._feat_dir = None
        self._feat_dir_norm = None

        # Local parameters
        self._mode = None
        self._ov = ov
        self._split = split
        self._db = db
        self._nfft = nfft
        self._win_len = self._nfft
        self._hop_len = self._nfft//2
        self._dataset = dataset
        self._eps = np.spacing(np.float(1e-16))

        # If circular-array 8 channels else 4 for Ambisonic
        if 'c' in self._dataset:
            self._nb_channels = 8
        else:
            self._nb_channels = 4

        # Sound event classes dictionary
        self._unique_classes = dict()
        if 'real' in self._dataset:
            # Urbansound8k sound events
            self._unique_classes = \
                {
                    '1': 0,
                    '3': 1,
                    '4': 2,
                    '5': 3,
                    '6': 4,
                    '7': 5,
                    '8': 6,
                    '9': 7
                }
        else:
            # DCASE 2016 Task 2 sound events
            self._unique_classes = \
                {
                    'clearthroat': 2,
                    'cough': 8,
                    'doorslam': 9,
                    'drawer': 1,
                    'keyboard': 6,
                    'keysDrop': 4,
                    'knock': 0,
                    'laughter': 10,
                    'pageturn': 7,
                    'phone': 3,
                    'speech': 5
                }

        self._fs = 44100
        self._frame_res = self._fs / float(self._hop_len)
        self._hop_len_s = self._nfft/2.0/self._fs
        self._nb_frames_1s = int(1 / self._hop_len_s)
        self._fade_win_size = 0.01 * self._fs

        self._resolution = 10
        self._azi_list = range(-180, 180, self._resolution)
        self._length = len(self._azi_list)
        self._ele_list = range(-60, 60, self._resolution)
        self._height = len(self._ele_list)
        self._weakness = None

        # For regression task only
        self._default_azi = 180
        self._default_ele = 60

        if self._default_azi in self._azi_list:
            print('ERROR: chosen default_azi value {} should not exist in azi_list'.format(self._default_azi))
            exit()
        if self._default_ele in self._ele_list:
            print('ERROR: chosen default_ele value {} should not exist in ele_list'.format(self._default_ele))
            exit()

        self._audio_max_len_samples = 30 * self._fs  # TODO: Fix the audio synthesis code to always generate 30s of
        # audio. Currently it generates audio till the last active sound event, which is not always 30s long. This is a
        # quick fix to overcome that. We need this because, for processing and training we need the length of features
        # to be fixed.

        self._max_frames = int(np.ceil((self._audio_max_len_samples - self._win_len) / float(self._hop_len)))
Ejemplo n.º 36
0
    def setup_train(self):

        # dimensions: (batch, time, notes, input_data) with input_data as in architecture
        self.input_mat = T.btensor4()
        # dimensions: (batch, time, notes, onOrArtic) with 0:on, 1:artic
        self.output_mat = T.btensor4()

        self.epsilon = np.spacing(np.float32(1.0))

        def step_time(in_data, *other):
            other = list(other)
            split = -len(self.t_layer_sizes) if self.dropout else len(other)
            hiddens = other[:split]
            masks = [None] + other[split:] if self.dropout else []
            new_states = self.time_model.forward(in_data,
                                                 prev_hiddens=hiddens,
                                                 dropout=masks)
            return new_states

        def step_note(in_data, *other):
            other = list(other)
            split = -len(self.p_layer_sizes) if self.dropout else len(other)
            hiddens = other[:split]
            masks = [None] + other[split:] if self.dropout else []
            new_states = self.pitch_model.forward(in_data,
                                                  prev_hiddens=hiddens,
                                                  dropout=masks)
            return new_states

        # We generate an output for each input, so it doesn't make sense to use the last output as an input.
        # Note that we assume the sentinel start value is already present
        # TEMP CHANGE: NO SENTINEL
        input_slice = self.input_mat[:, 0:-1]
        n_batch, n_time, n_note, n_ipn = input_slice.shape

        # time_inputs is a matrix (time, batch/note, input_per_note)
        time_inputs = input_slice.transpose((1, 0, 2, 3)).reshape(
            (n_time, n_batch * n_note, n_ipn))
        num_time_parallel = time_inputs.shape[1]

        # apply dropout
        if self.dropout > 0:
            time_masks = theano_lstm.MultiDropout(
                [(num_time_parallel, shape) for shape in self.t_layer_sizes],
                self.dropout)
        else:
            time_masks = []

        time_outputs_info = [
            initial_state_with_taps(layer, num_time_parallel)
            for layer in self.time_model.layers
        ]
        time_result, _ = theano.scan(fn=step_time,
                                     sequences=[time_inputs],
                                     non_sequences=time_masks,
                                     outputs_info=time_outputs_info)

        self.time_thoughts = time_result

        # Now time_result is a list of matrix [layer](time, batch/note, hidden_states) for each layer but we only care about
        # the hidden state of the last layer.
        # Transpose to be (note, batch/time, hidden_states)
        last_layer = get_last_layer(time_result)
        n_hidden = last_layer.shape[2]
        time_final = get_last_layer(time_result).reshape(
            (n_time, n_batch, n_note, n_hidden)).transpose(
                (2, 1, 0, 3)).reshape((n_note, n_batch * n_time, n_hidden))

        # note_choices_inputs represents the last chosen note. Starts with [0,0], doesn't include last note.
        # In (note, batch/time, 2) format
        # Shape of start is thus (1, N, 2), concatenated with all but last element of output_mat transformed to (x, N, 2)
        start_note_values = T.alloc(0, 1, time_final.shape[1], 2)
        correct_choices = self.output_mat[:, 1:, 0:-1, :].transpose(
            (2, 0, 1, 3)).reshape((n_note - 1, n_batch * n_time, 2))
        note_choices_inputs = T.concatenate(
            [start_note_values, correct_choices], axis=0)

        # Together, this and the output from the last LSTM goes to the new LSTM, but rotated, so that the batches in
        # one direction are the steps in the other, and vice versa.
        note_inputs = T.concatenate([time_final, note_choices_inputs], axis=2)
        num_timebatch = note_inputs.shape[1]

        # apply dropout
        if self.dropout > 0:
            pitch_masks = theano_lstm.MultiDropout(
                [(num_timebatch, shape) for shape in self.p_layer_sizes],
                self.dropout)
        else:
            pitch_masks = []

        note_outputs_info = [
            initial_state_with_taps(layer, num_timebatch)
            for layer in self.pitch_model.layers
        ]
        note_result, _ = theano.scan(fn=step_note,
                                     sequences=[note_inputs],
                                     non_sequences=pitch_masks,
                                     outputs_info=note_outputs_info)

        self.note_thoughts = note_result

        # Now note_result is a list of matrix [layer](note, batch/time, onOrArticProb) for each layer but we only care about
        # the hidden state of the last layer.
        # Transpose to be (batch, time, note, onOrArticProb)
        note_final = get_last_layer(note_result).reshape(
            (n_note, n_batch, n_time, 2)).transpose(1, 2, 0, 3)

        # The cost of the entire procedure is the negative log likelihood of the events all happening.
        # For the purposes of training, if the ouputted probability is P, then the likelihood of seeing a 1 is P, and
        # the likelihood of seeing 0 is (1-P). So the likelihood is (1-P)(1-x) + Px = 2Px - P - x + 1
        # Since they are all binary decisions, and are all probabilities given all previous decisions, we can just
        # multiply the likelihoods, or, since we are logging them, add the logs.

        # Note that we mask out the articulations for those notes that aren't played, because it doesn't matter
        # whether or not those are articulated.
        # The padright is there because self.output_mat[:,:,:,0] -> 3D matrix with (b,x,y), but we need 3d tensor with
        # (b,x,y,1) instead
        active_notes = T.shape_padright(self.output_mat[:, 1:, :, 0])
        mask = T.concatenate([T.ones_like(active_notes), active_notes], axis=3)

        loglikelihoods = mask * T.log(2 * note_final * self.output_mat[:, 1:] -
                                      note_final - self.output_mat[:, 1:] + 1 +
                                      self.epsilon)
        self.cost = T.neg(T.sum(loglikelihoods))

        updates, _, _, _, _ = create_optimization_updates(self.cost,
                                                          self.params,
                                                          method="adadelta")
        self.update_fun = theano.function(
            inputs=[self.input_mat, self.output_mat],
            outputs=self.cost,
            updates=updates,
            allow_input_downcast=True)

        self.update_thought_fun = theano.function(
            inputs=[self.input_mat, self.output_mat],
            outputs=ensure_list(self.time_thoughts) +
            ensure_list(self.note_thoughts) + [self.cost],
            allow_input_downcast=True)
Ejemplo n.º 37
0
def _ars_compute_hulls(S, fS, domain):
    # compute lower piecewise-linear hull
    # if the domain of func is unbounded to the left or right, then the lower
    # hull takes on -inf to the left or right of the end points of S
    lowerHull = []
    for li in np.arange(len(S) - 1):
        h = Hull()
        h.m = (fS[li + 1] - fS[li]) / (S[li + 1] - S[li])
        h.b = fS[li] - h.m * S[li]
        h.left = S[li]
        h.right = S[li + 1]
        lowerHull.append(h)

    # compute upper piecewise-linear hull
    upperHull = []

    if np.isinf(domain[0]):
        # first line (from -infinity)
        m = (fS[1] - fS[0]) / (S[1] - S[0])
        b = fS[0] - m * S[0]
        # pro = np.exp(b)/m * ( np.exp(m*S[0]) - 0 ) # integrating in from -infinity
        lnpr = b - np.log(m) + m * S[0]
        h = Hull()
        h.m = m
        h.b = b
        h.lnpr = lnpr
        h.left = -np.Inf
        h.right = S[0]
        upperHull.append(h)

    # second line
    m = (fS[2] - fS[1]) / (S[2] - S[1])
    b = fS[1] - m * S[1]
    # pro = np.exp(b)/m * ( np.exp(m*S[1]) - np.exp(m*S[0]) )
    lnpr = _signed_lse(m, b, S[1], S[0])
    # Append upper hull for second line
    h = Hull()
    h.m = m
    h.b = b
    h.lnpr = lnpr
    h.left = S[0]
    h.right = S[1]
    upperHull.append(h)

    # interior lines
    # there are two lines between each abscissa
    for li in np.arange(1, len(S) - 2):

        m1 = (fS[li] - fS[li - 1]) / (S[li] - S[li - 1])
        b1 = fS[li] - m1 * S[li]

        m2 = (fS[li + 2] - fS[li + 1]) / (S[li + 2] - S[li + 1])
        b2 = fS[li + 1] - m2 * S[li + 1]

        # compute the two lines' intersection
        # Make sure it's in the valid range
        ix = (b1 - b2) / (m2 - m1)
        if not (ix >= S[li] and ix <= S[li + 1]):
            # This seems to happen when fS goes from a reasonable to an unreasonable range
            # import pdb; pdb.set_trace()
            ix = np.clip(ix, S[li] + np.spacing(1), S[li + 1] - np.spacing(1))

        # pro = np.exp(b1)/m1 * ( np.exp(m1*ix) - np.exp(m1*S[li]) )
        lnpr1 = _signed_lse(m1, b1, ix, S[li])
        h = Hull()
        h.m = m1
        h.b = b1
        h.lnpr = lnpr1
        h.left = S[li]
        h.right = ix
        upperHull.append(h)

        # pro = np.exp(b2)/m2 * ( np.exp(m2*S[li+1]) - np.exp(m2*ix) )
        lnpr2 = _signed_lse(m2, b2, S[li + 1], ix)
        h = Hull()
        h.m = m2
        h.b = b2
        h.lnpr = lnpr2
        h.left = ix
        h.right = S[li + 1]
        upperHull.append(h)

    # second to last line (m<0)
    m = (fS[-2] - fS[-3]) / (S[-2] - S[-3])
    b = fS[-2] - m * S[-2]
    # pro = np.exp(b)/m * ( np.exp(m*S[-1]) - np.exp(m*S[-2]) )
    lnpr = _signed_lse(m, b, S[-1], S[-2])
    h = Hull()
    h.m = m
    h.b = b
    h.lnpr = lnpr
    h.left = S[-2]
    h.right = S[-1]
    upperHull.append(h)

    if np.isinf(domain[1]):
        # last line (to infinity)
        m = (fS[-1] - fS[-2]) / (S[-1] - S[-2])
        b = fS[-1] - m * S[-1]
        # pro = np.exp(b)/m * ( 0 - np.exp(m*S[-1]) )
        lnpr = b - np.log(np.abs(m)) + m * S[-1]
        h = Hull()
        h.m = m
        h.b = b
        h.lnpr = lnpr
        h.left = S[-1]
        h.right = np.Inf
        upperHull.append(h)

    lnprs = np.array([h.lnpr for h in upperHull])
    lnZ = logsumexp(lnprs)
    prs = np.exp(lnprs - lnZ)
    for (i, h) in enumerate(upperHull):
        h.pr = prs[i]

    if not np.all(np.isfinite(prs)):
        raise Exception("ARS prs contains Inf or NaN")

    return lowerHull, upperHull
Ejemplo n.º 38
0
import numpy as np

EPSILON = np.spacing(1)
SPATIAL_DIMENSIONS = 2, 3, 4


class TverskyLoss:
    def __init__(self, *, alpha, beta, epsilon=None):
        self.alpha = alpha
        self.beta = beta
        self.epsilon = EPSILON if epsilon is None else epsilon

    def __call__(self, output, target):
        loss = get_tversky_loss(
            output,
            target,
            self.alpha,
            self.beta,
            epsilon=self.epsilon,
        )
        return loss


class DiceLoss(TverskyLoss):
    def __init__(self, epsilon=None):
        super().__init__(alpha=0.5, beta=0.5, epsilon=epsilon)


def get_confusion(output, target):
    if output.shape != target.shape:
        message = (
from numpy import spacing, prod, fromfile, exp, log, inf, isfinite
from numpy.linalg import norm, inv
from sys import stdout
from os import remove
from os.path import exists, expanduser, isdir
from time import time
from numpy.random import RandomState
from numpy.lib.stride_tricks import as_strided
from warnings import filterwarnings
import warnings
from Params import Params
from types import IntType, LongType, FloatType, StringType
NumType = (IntType, LongType, FloatType)

# Small number
eps = spacing(1.)
"""
Normalize stimulus
    stim: numpy array with sample number as last dimension
    pixelNorm: If True, normalize each location by individual mean and stdev.
       If a tuple, use first element as mean and second as stdev.
       Otherwise, normalize using global statistics.
    Returns normalized stimulus, mean, and stdev.
"""


def normStim(stim, pixelNorm=True):

    # Ignore divide by zero warnings
    filterwarnings('ignore', 'invalid value encountered in divide')
Ejemplo n.º 40
0
def gnmf(X,
         A,
         lambd=0,
         n_components=None,
         tol_nmf=1e-3,
         max_iter=100,
         verbose=False):

    X = check_array(X)
    check_non_negative(X, "NMF.fit")
    n_samples, n_features = X.shape

    if not n_components:
        n_components = min(n_samples, n_features)
    else:
        n_components = n_components

    W, H = NBS_init(X, n_components, init='nndsvd')

    list_reconstruction_err_ = []
    reconstruction_err_ = LA.norm(X - np.dot(W, H))
    list_reconstruction_err_.append(reconstruction_err_)

    eps = np.spacing(1)  # 2.2204460492503131e-16
    Lp = np.matrix(np.diag(np.asarray(A).sum(axis=0)))  # degree matrix
    Lm = A

    for n_iter in range(1, max_iter + 1):

        if verbose:
            print(
                "Iteration ={:4d} / {:d} - - - - — Error = {:.2f} - - - - — Tolerance = {:f}"
                .format(n_iter, max_iter, reconstruction_err_, tol_nmf))

        h1 = lambd * np.dot(H, Lm) + np.dot(W.T,
                                            (X + eps) / (np.dot(W, H) + eps))
        h2 = lambd * np.dot(H, Lp) + np.dot(W.T, np.ones(X.shape))
        H = np.multiply(H, (h1 + eps) / (h2 + eps))
        H[H <= 0] = eps
        H[np.isnan(H)] = eps

        w1 = np.dot((X + eps) / (np.dot(W, H) + eps), H.T)
        w2 = np.dot(np.ones(X.shape), H.T)
        W = np.multiply(W, (w1 + eps) / (w2 + eps))
        W[W <= 0] = eps
        W[np.isnan(W)] = eps

        if reconstruction_err_ > LA.norm(X - np.dot(W, H)):
            H = (1 - eps) * H + eps * np.random.normal(
                0, 1, (n_components, n_features))**2
            W = (1 - eps) * W + eps * np.random.normal(
                0, 1, (n_samples, n_components))**2
        reconstruction_err_ = LA.norm(X - np.dot(W, H))
        list_reconstruction_err_.append(reconstruction_err_)

        if reconstruction_err_ < tol_nmf:
            warnings.warn("Tolerance error reached during fit")
            break

        if np.isnan(W).any() or np.isnan(H).any():
            warnings.warn("NaN values at " + str(n_iter) + " Error=" +
                          str(reconstruction_err_))
            break

        if n_iter == max_iter:
            warnings.warn("Iteration limit reached during fit")

    return (np.squeeze(np.asarray(W)), np.squeeze(np.asarray(H)),
            list_reconstruction_err_)
Ejemplo n.º 41
0
def AngSetupFSC(X, Y, bX, bY, bZ, PA, PB, nu):
    """
    AngSetupSurf calculates the displacements associated with an angular
    dislocation pair on each side of an RD in a half-space.
    """

    SideVec = PB - PA
    eZ = numpy.array([0, 0, 1])
    beta = numpy.arccos(-SideVec.dot(eZ) / norm(SideVec))

    eps = numpy.spacing(
        1)  # distance between 1 and the nearest floating point number
    if numpy.abs(beta) < eps or numpy.abs(numpy.pi - beta) < eps:
        ue = numpy.zeros(numpy.shape(X))
        un = numpy.zeros(numpy.shape(X))
        uv = numpy.zeros(numpy.shape(X))
    else:
        ey1 = numpy.array([*SideVec[0:2], 0])
        ey1 = ey1 / norm(ey1)
        ey3 = -eZ
        ey2 = numpy.cross(ey3, ey1)
        A = numpy.array([ey1, ey2, ey3])  # Transformation matrix

        # Transform coordinates from EFCS to the first ADCS
        [y1A, y2A, unused] = CoordTrans(X - PA[0], Y - PA[1],
                                        numpy.zeros(X.size) - PA[2], A)
        # Transform coordinates from EFCS to the second ADCS
        [y1AB, y2AB, unused] = CoordTrans(SideVec[0], SideVec[1], SideVec[2],
                                          A)
        y1B = y1A - y1AB
        y2B = y2A - y2AB

        # Transform slip vector components from EFCS to ADCS
        [b1, b2, b3] = CoordTrans(bX, bY, bZ, A)

        # Determine the best artefact-free configuration for the calculation
        # points near the free surface
        I = (beta * y1A) >= 0
        J = numpy.logical_not(I)

        v1A = numpy.zeros(I.shape)
        v2A = numpy.zeros(I.shape)
        v3A = numpy.zeros(I.shape)
        v1B = numpy.zeros(I.shape)
        v2B = numpy.zeros(I.shape)
        v3B = numpy.zeros(I.shape)

        # Configuration I
        v1A[I], v2A[I], v3A[I] = AngDisDispSurf(y1A[I], y2A[I],
                                                -numpy.pi + beta, b1, b2, b3,
                                                nu, -PA[2])
        v1B[I], v2B[I], v3B[I] = AngDisDispSurf(y1B[I], y2B[I],
                                                -numpy.pi + beta, b1, b2, b3,
                                                nu, -PB[2])

        # Configuration II
        v1A[J], v2A[J], v3A[J] = AngDisDispSurf(y1A[J], y2A[J], beta, b1, b2,
                                                b3, nu, -PA[2])
        v1B[J], v2B[J], v3B[J] = AngDisDispSurf(y1B[J], y2B[J], beta, b1, b2,
                                                b3, nu, -PB[2])

        # Calculate total displacements in ADCS
        v1 = v1B - v1A
        v2 = v2B - v2A
        v3 = v3B - v3A

        # Transform total displacements from ADCS to EFCS
        [ue, un, uv] = CoordTrans(v1, v2, v3, A.transpose())

    return ue, un, uv
Ejemplo n.º 42
0
def RandomLearning(LPATH, LFILE, LCNT, b, color, idn):
    def print2f(MSG):
        lf = open(LOGPATH + LFILE + '_out' + idn + '_random.txt', 'a')
        print >> lf, MSG
        lf.close()

    ff = open(LPATH + LFILE, 'r')
    idx = 0
    fRNA = np.zeros((LCNT, 23 * 4))
    label = np.zeros((LCNT, ))
    for line in ff:
        f = line.split('\t')
        if (int(f[1]) == 1):
            label[idx] = 1
        else:
            label[idx] = -1
        fRNA[idx] = Ronehot(f[0])
        idx += 1

    X_train, X_test, y_train, y_test = train_test_split(fRNA,
                                                        label,
                                                        test_size=0.2,
                                                        random_state=0)
    print2f((np.shape(X_train), np.shape(y_train), np.shape(X_test),
             np.shape(y_test)))

    TRAINLEN = np.shape(X_train)[0]
    INTLEN = int(TRAINLEN * 0.01)

    aa = np.split(X_train, [TRAINLEN - INTLEN, TRAINLEN - INTLEN])
    X_train = aa[0]
    inttrain_x = aa[2]
    aa = np.split(y_train, [TRAINLEN - INTLEN, TRAINLEN - INTLEN])
    y_train = aa[0]
    inttrain_y = aa[2]

    TRAINLEN = np.shape(X_train)[0]
    INTLEN = np.shape(inttrain_x)[0]
    AUGLEN = TRAINLEN * 16

    Uset = set()
    Lset = set()
    for i in range(0, INTLEN):
        Lset.add(i)
    for i in range(INTLEN, TRAINLEN):
        Uset.add(i)

    train_x = np.zeros((AUGLEN, 23 * 4))
    train_y = np.zeros((AUGLEN, ))
    train_l = np.zeros((AUGLEN, ))
    patch = [set() for x in range(0, TRAINLEN)]

    for i in range(0, TRAINLEN):
        sample = X_train[i]
        R1 = np.zeros((4))
        R2 = np.zeros((4))
        for j in range(0, 4):
            R1[j] = 1
            for k in range(0, 4):
                R2[k] = 1
                RR = np.concatenate((R1, R2))
                for x in range(0, 8):
                    sample[x] = RR[x]
                train_x[i * 16 + j * 4 + k] = sample
                train_y[i * 16 + j * 4 + k] = y_train[i]
                train_l[i * 16 + j * 4 + k] = i
                patch[i].add(i * 16 + j * 4 + k)
                R2[k] = 0
            R1[j] = 0

    print2f((TRAINLEN, AUGLEN, INTLEN))
    print2f(
        (np.shape(X_train)[0], np.shape(train_x)[0], np.shape(inttrain_x)[0]))
    print2f((patch[0]))

    clf = GradientBoostingClassifier().fit(inttrain_x, inttrain_y)
    print2f(("init: ", clf.score(X_test, y_test)))
    clf2 = GradientBoostingClassifier().fit(X_train, y_train)
    print2f(("complete: ", clf2.score(X_test, y_test)))

    #for i in range(10,20):
    #    print (clf.predict(X_test[i]), clf.predict_proba(X_test[i])[0][1], clf.predict_log_proba(X_test[i])[0][1], math.log(clf.predict_proba(X_test[i])[0][1]), y_test[i])

    eps = np.spacing(1)
    ITER = int(TRAINLEN / b)
    patchsize = 16
    predpatch = [0.0 for x in range(0, patchsize)]
    ACC = []
    ITR = []
    LAB = []

    for IT in range(0, ITER):
        if (INTLEN + b > TRAINLEN):
            print2f(("OUT OF RANGE "))
            break
        Rm = random.sample(Uset, b)
        for elm in Rm:
            Lset.add(elm)
            Uset.remove(elm)
            inttrain_x = np.concatenate((inttrain_x, [X_train[elm]]), axis=0)
            inttrain_y = np.concatenate((inttrain_y, [y_train[elm]]), axis=0)
            INTLEN += 1
        print2f((np.shape(inttrain_x)[0], len(Lset), len(Uset)))
        clf = GradientBoostingClassifier().fit(inttrain_x, inttrain_y)
        res = clf.score(X_test, y_test)
        print2f(("iter: ", IT, res))
        ACC.append(res)
        ITR.append(IT)
        LAB.append(len(Lset))

    plt.plot(LAB, ACC, color)
    #plt.plot(LAB,ACC,'b*')
    plt.xlabel('Num of labels')
    plt.ylabel('Accuracy')
    plt.ylim(0.5, 1.0)
    plt.title(LFILE)
    plt.show()
    def _valid_epoch(self, epoch):
        if self.val_loader is None:
            if self.gpu == 0:
                self.logger.warning(
                    'Not data loader was passed for the validation step, No validation is performed !'
                )
            return {}

        if self.gpu == 0:
            self.logger.info('\n###### EVALUATION ######')

        self.model.eval()
        self.wrt_mode = 'val'

        total_loss_val = AverageMeter()
        total_inter, total_union = 0, 0
        total_correct, total_label = 0, 0

        tbar = tqdm(self.val_loader, ncols=130)
        with torch.no_grad():
            if self.gpu == 0:
                val_visual = []

            for batch_idx, (data, target) in enumerate(tbar):
                target, data = target.cuda(non_blocking=True), data.cuda(
                    non_blocking=True)

                H, W = target.size(1), target.size(2)
                up_sizes = (ceil(H / 8) * 8, ceil(W / 8) * 8)
                pad_h, pad_w = up_sizes[0] - data.size(
                    2), up_sizes[1] - data.size(3)
                data = F.pad(data, pad=(0, pad_w, 0, pad_h), mode='reflect')

                output = self.model(data)

                output = output[:, :, :H, :W]
                # LOSS
                loss = F.cross_entropy(output,
                                       target,
                                       ignore_index=self.ignore_index)

                total_loss_val.update(loss.item())

                # eval_metrics has already implemented DDP synchronized
                correct, labeled, inter, union = eval_metrics(
                    output, target, self.num_classes, self.ignore_index)

                total_inter, total_union = total_inter + inter, total_union + union
                total_correct, total_label = total_correct + correct, total_label + labeled

                if self.gpu == 0:
                    # LIST OF IMAGE TO VIZ (15 images)
                    if len(val_visual) < 15:
                        if isinstance(data, list): data = data[0]
                        target_np = target.data.cpu().numpy()
                        output_np = output.data.max(1)[1].cpu().numpy()
                        val_visual.append(
                            [data[0].data.cpu(), target_np[0], output_np[0]])

                # PRINT INFO
                pixAcc = 1.0 * total_correct / (np.spacing(1) + total_label)
                IoU = 1.0 * total_inter / (np.spacing(1) + total_union)
                mIoU = IoU.mean()
                seg_metrics = {
                    "Pixel_Accuracy":
                    np.round(pixAcc, 3),
                    "Mean_IoU":
                    np.round(mIoU, 3),
                    "Class_IoU":
                    dict(zip(range(self.num_classes), np.round(IoU, 3)))
                }

                if self.gpu == 0:
                    tbar.set_description(
                        'EVAL ({}) | Loss: {:.3f}, PixelAcc: {:.2f}, Mean IoU: {:.2f} |'
                        .format(epoch, total_loss_val.average, pixAcc, mIoU))

            if self.gpu == 0:
                self._add_img_tb(val_visual, 'val')

                # METRICS TO TENSORBOARD
                self.wrt_step = (epoch) * len(self.val_loader)
                self.writer.add_scalar(f'{self.wrt_mode}/loss',
                                       total_loss_val.average, self.wrt_step)
                for k, v in list(seg_metrics.items())[:-1]:
                    self.writer.add_scalar(f'{self.wrt_mode}/{k}', v,
                                           self.wrt_step)

            log = {'val_loss': total_loss_val.average, **seg_metrics}

        return log
Ejemplo n.º 44
0
    def accumulate(self, p=None):
        '''
        Accumulate per image evaluation results and store the result in self.eval
        :param p: input params for evaluation
        :return: None
        '''
        print('Accumulating evaluation results...')
        tic = time.time()
        if not self.evalImgs:
            print('Please run evaluate() first')
        # allows input customized parameters
        if p is None:
            p = self.params
        p.catIds = p.catIds if p.useCats == 1 else [-1]
        T = len(p.iouThrs)
        R = len(p.recThrs)
        K = len(p.catIds) if p.useCats else 1
        A = len(p.areaRng)
        M = len(p.maxDets)
        precision = -np.ones(
            (T, R, K, A, M))  # -1 for the precision of absent categories
        recall = -np.ones((T, K, A, M))
        scores = -np.ones((T, R, K, A, M))

        # create dictionary for future indexing
        _pe = self._paramsEval
        catIds = _pe.catIds if _pe.useCats else [-1]
        setK = set(catIds)
        setA = set(map(tuple, _pe.areaRng))
        setM = set(_pe.maxDets)
        setI = set(_pe.imgIds)
        # get inds to evaluate
        k_list = [n for n, k in enumerate(p.catIds) if k in setK]
        m_list = [m for n, m in enumerate(p.maxDets) if m in setM]
        a_list = [
            n for n, a in enumerate(map(lambda x: tuple(x), p.areaRng))
            if a in setA
        ]
        i_list = [n for n, i in enumerate(p.imgIds) if i in setI]
        I0 = len(_pe.imgIds)
        A0 = len(_pe.areaRng)
        # retrieve E at each category, area range, and max number of detections
        for k, k0 in enumerate(k_list):
            Nk = k0 * A0 * I0
            for a, a0 in enumerate(a_list):
                Na = a0 * I0
                for m, maxDet in enumerate(m_list):
                    E = [self.evalImgs[Nk + Na + i] for i in i_list]
                    E = [e for e in E if not e is None]
                    if len(E) == 0:
                        continue
                    dtScores = np.concatenate(
                        [e['dtScores'][0:maxDet] for e in E])

                    # different sorting method generates slightly different results.
                    # mergesort is used to be consistent as Matlab implementation.
                    inds = np.argsort(-dtScores, kind='mergesort')
                    dtScoresSorted = dtScores[inds]

                    dtm = np.concatenate(
                        [e['dtMatches'][:, 0:maxDet] for e in E], axis=1)[:,
                                                                          inds]
                    dtIg = np.concatenate(
                        [e['dtIgnore'][:, 0:maxDet] for e in E], axis=1)[:,
                                                                         inds]
                    gtIg = np.concatenate([e['gtIgnore'] for e in E])
                    npig = np.count_nonzero(gtIg == 0)
                    if npig == 0:
                        continue
                    tps = np.logical_and(dtm, np.logical_not(dtIg))
                    fps = np.logical_and(np.logical_not(dtm),
                                         np.logical_not(dtIg))

                    tp_sum = np.cumsum(tps, axis=1).astype(dtype=np.float)
                    fp_sum = np.cumsum(fps, axis=1).astype(dtype=np.float)
                    for t, (tp, fp) in enumerate(zip(tp_sum, fp_sum)):
                        tp = np.array(tp)
                        fp = np.array(fp)
                        nd = len(tp)
                        rc = tp / npig
                        pr = tp / (fp + tp + np.spacing(1))
                        q = np.zeros((R, ))
                        ss = np.zeros((R, ))

                        if nd:
                            recall[t, k, a, m] = rc[-1]
                        else:
                            recall[t, k, a, m] = 0

                        # numpy is slow without cython optimization for accessing elements
                        # use python array gets significant speed improvement
                        pr = pr.tolist()
                        q = q.tolist()

                        #To get a zig-zag PR curve - comment the loop below

                        #for i in range(nd-1, 0, -1):
                        #    if pr[i] > pr[i-1]:
                        #        pr[i-1] = pr[i]

                        inds = np.searchsorted(rc, p.recThrs, side='left')
                        try:
                            for ri, pi in enumerate(inds):
                                q[ri] = pr[pi]
                                ss[ri] = dtScoresSorted[pi]
                        except:
                            pass
                        precision[t, :, k, a, m] = np.array(q)
                        scores[t, :, k, a, m] = np.array(ss)
        self.eval = {
            'params': p,
            'counts': [T, R, K, A, M],
            'date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            'precision': precision,
            'recall': recall,
            'scores': scores,
        }
        toc = time.time()
        print('DONE (t={:0.2f}s).'.format(toc - tic))
Ejemplo n.º 45
0
def weighted_average_slopes(data, old_start, old_dt, new_start, new_dt,
                            new_npts, *args, **kwargs):
    r"""
    Implements the weighted average slopes interpolation scheme proposed in
    [Wiggins1976]_ for evenly sampled data. The scheme guarantees that there
    will be no additional extrema after the interpolation in contrast to
    spline interpolation.

    The slope :math:`s_i` at each knot is given by a weighted average of the
    adjacent linear slopes :math:`m_i` and :math:`m_{i+j}`:

    .. math::

        s_i = (w_i m_i + w_{i+1} m_{i+1}) / (w_i + w_{i+1})

    where

    .. math::

        w = 1 / max \left\{ \left\| m_i \\right\|, \epsilon \\right\}

    The value at each data point and the slope are then plugged into a
    piecewise continuous cubic polynomial used to evaluate the interpolated
    sample points.

    :type data: array_like
    :param data: Array to interpolate.
    :type old_start: float
    :param old_start: The start of the array as a number.
    :type old_start: float
    :param old_dt: The time delta of the current array.
    :type new_start: float
    :param new_start: The start of the interpolated array. Must be greater
        or equal to the current start of the array.
    :type new_dt: float
    :param new_dt: The desired new time delta.
    :type new_npts: int
    :param new_npts: The new number of samples.
    """
    old_end, new_end = _validate_parameters(data, old_start, old_dt,
                                            new_start, new_dt, new_npts)
    # In almost all cases the unit will be in time.
    new_time_array = np.linspace(new_start, new_end, new_npts)

    m = np.diff(data) / old_dt
    w = np.abs(m)
    w = 1.0 / np.clip(w, np.spacing(1), w.max())

    slope = np.empty(len(data), dtype=np.float64)
    slope[0] = m[0]
    slope[1:-1] = (w[:-1] * m[:-1] + w[1:] * m[1:]) / (w[:-1] + w[1:])
    slope[-1] = m[-1]

    # If m_i and m_{i+1} have opposite signs then set the slope to zero.
    # This forces the curve to have extrema at the sample points and not
    # in-between.
    sign_change = np.diff(np.sign(m)).astype(np.bool)
    slope[1:-1][sign_change] = 0.0

    derivatives = np.empty((len(data), 2), dtype=np.float64)
    derivatives[:, 0] = data
    derivatives[:, 1] = slope

    # Create interpolated value using hermite interpolation. In this case
    # it is directly applicable as the first derivatives are known.
    # Using scipy.interpolate.piecewise_polynomial_interpolate() is too
    # memory intensive
    return_data = np.empty(len(new_time_array), dtype=np.float64)
    clibsignal.hermite_interpolation(data, slope, new_time_array, return_data,
                                     len(data), len(return_data), old_dt,
                                     old_start)
    return return_data
Ejemplo n.º 46
0
    predict(i, future, trainlen)
print("--- %s seconds ---" % (time.time() - start_time))
rc("text", usetex=False)
plt.figure(figsize=(16, 8))
plt.plot(
    range(0, trainlen + futureTotal),
    y[1000:trainlen + futureTotal],
    "b",
    label="Data",
    alpha=0.3,
)
# plt.plot(range(0,trainlen),pred_training,'.g',  alpha=0.3)
plt.plot(
    range(trainlen, trainlen + futureTotal),
    predictedTotal,
    "k",
    alpha=0.8,
    label="Free Running ESN",
)

lo, hi = plt.ylim()
plt.plot([trainlen, trainlen], [lo + np.spacing(1), hi - np.spacing(1)],
         "k:",
         linewidth=4)

plt.title(r"Ground Truth and Echo State Network Output", fontsize=25)
plt.xlabel(r"Time (Days)", fontsize=20, labelpad=10)
plt.ylabel(r"Price ($)", fontsize=20, labelpad=10)
plt.legend(fontsize="xx-large", loc="best")
plt.savefig("/home/homeuser/Stonks/ESN.png")
Ejemplo n.º 47
0
    def test_half_fpe(self):
        with np.errstate(all='raise'):
            sx16 = np.array((1e-4, ), dtype=float16)
            bx16 = np.array((1e4, ), dtype=float16)
            sy16 = float16(1e-4)
            by16 = float16(1e4)

            # Underflow errors
            assert_raises_fpe('underflow', lambda a, b: a * b, sx16, sx16)
            assert_raises_fpe('underflow', lambda a, b: a * b, sx16, sy16)
            assert_raises_fpe('underflow', lambda a, b: a * b, sy16, sx16)
            assert_raises_fpe('underflow', lambda a, b: a * b, sy16, sy16)
            assert_raises_fpe('underflow', lambda a, b: a / b, sx16, bx16)
            assert_raises_fpe('underflow', lambda a, b: a / b, sx16, by16)
            assert_raises_fpe('underflow', lambda a, b: a / b, sy16, bx16)
            assert_raises_fpe('underflow', lambda a, b: a / b, sy16, by16)
            assert_raises_fpe('underflow', lambda a, b: a / b,
                              float16(2.**-14), float16(2**11))
            assert_raises_fpe('underflow', lambda a, b: a / b,
                              float16(-2.**-14), float16(2**11))
            assert_raises_fpe('underflow', lambda a, b: a / b,
                              float16(2.**-14 + 2**-24), float16(2))
            assert_raises_fpe('underflow', lambda a, b: a / b,
                              float16(-2.**-14 - 2**-24), float16(2))
            assert_raises_fpe('underflow', lambda a, b: a / b,
                              float16(2.**-14 + 2**-23), float16(4))

            # Overflow errors
            assert_raises_fpe('overflow', lambda a, b: a * b, bx16, bx16)
            assert_raises_fpe('overflow', lambda a, b: a * b, bx16, by16)
            assert_raises_fpe('overflow', lambda a, b: a * b, by16, bx16)
            assert_raises_fpe('overflow', lambda a, b: a * b, by16, by16)
            assert_raises_fpe('overflow', lambda a, b: a / b, bx16, sx16)
            assert_raises_fpe('overflow', lambda a, b: a / b, bx16, sy16)
            assert_raises_fpe('overflow', lambda a, b: a / b, by16, sx16)
            assert_raises_fpe('overflow', lambda a, b: a / b, by16, sy16)
            assert_raises_fpe('overflow', lambda a, b: a + b, float16(65504),
                              float16(17))
            assert_raises_fpe('overflow', lambda a, b: a - b, float16(-65504),
                              float16(17))
            assert_raises_fpe('overflow', np.nextafter, float16(65504),
                              float16(np.inf))
            assert_raises_fpe('overflow', np.nextafter, float16(-65504),
                              float16(-np.inf))
            assert_raises_fpe('overflow', np.spacing, float16(65504))

            # Invalid value errors
            assert_raises_fpe('invalid', np.divide, float16(np.inf),
                              float16(np.inf))
            assert_raises_fpe('invalid', np.spacing, float16(np.inf))
            assert_raises_fpe('invalid', np.spacing, float16(np.nan))
            assert_raises_fpe('invalid', np.nextafter, float16(np.inf),
                              float16(0))
            assert_raises_fpe('invalid', np.nextafter, float16(-np.inf),
                              float16(0))
            assert_raises_fpe('invalid', np.nextafter, float16(0),
                              float16(np.nan))

            # These should not raise
            float16(65472) + float16(32)
            float16(2**-13) / float16(2)
            float16(2**-14) / float16(2**10)
            np.spacing(float16(-65504))
            np.nextafter(float16(65504), float16(-np.inf))
            np.nextafter(float16(-65504), float16(np.inf))
            float16(2**-14) / float16(2**10)
            float16(-2**-14) / float16(2**10)
            float16(2**-14 + 2**-23) / float16(2)
            float16(-2**-14 - 2**-23) / float16(2)
Ejemplo n.º 48
0
def getThresh(pks, doClip, pnorm=0.5):   
    """ Function for deciding threshold for peaks given heights of all peaks.
    
    Args:
        pks: 1-D array
            height of all peaks
            
        doClip: int
        
        pnorm: float, between 0 and 1, default is 0.5
            a variable deciding the amount of spikes chosen
    
    Returns:
        thresh: float
            threshold for choosing as spikes or not 
    
    """
    spread = np.array([pks.min(), pks.max()])
    spread = spread + np.diff(spread) * np.array([-0.05, 0.05])
    low_spk = False
    
    pts = np.linspace(spread[0], spread[1], 2001)
    KD = sm.nonparametric.KDEUnivariate(pks)
    KD.fit(bw='scott')
    f = KD.evaluate(pts)
    xi = pts
    center = np.where(xi>np.median(pks))[0][0]

    fmodel = np.concatenate([f[0:center+1], np.flipud(f[0:center])])
    if len(fmodel) < len(f):
        fmodel = np.append(fmodel, np.ones(len(f)-len(fmodel))*min(fmodel))
    else:
        fmodel = fmodel[0:len(f)]
       
    # adjust the model so it doesn't exceed the data:
    csf = np.cumsum(f) / np.sum(f)
    csmodel = np.cumsum(fmodel) / np.max([np.sum(f), np.sum(fmodel)])
    lastpt = np.where(np.logical_and(csf[0:-1]>csmodel[0:-1]+np.spacing(1), csf[1:]<csmodel[1:]))[0]
     
    if not lastpt.size:
        lastpt = center
    else:
        lastpt = lastpt[0]
        
    fmodel[0:lastpt+1] = f[0:lastpt+1]
    fmodel[lastpt:] = np.minimum(fmodel[lastpt:],f[lastpt:])
    
    csf = np.cumsum(f)
    csmodel = np.cumsum(fmodel)
    csf2 = csf[-1] - csf
    csmodel2 = csmodel[-1] - csmodel
    obj = csf2 ** pnorm - csmodel2 ** pnorm
    
    maxind = np.argmax(obj)
    thresh = xi[maxind]   
    
    if np.sum(pks>thresh)<30:
        low_spk = True
        print('Very few spikes were detected at the desired sensitivity/specificity tradeoff. Adjusting threshold to take 30 largest spikes')
        thresh = np.percentile(pks, 100*(1-30/len(pks)))
    elif ((np.sum(pks>thresh)>doClip) & (doClip>0)):
        print('Selecting top',doClip,'spikes for template')
        thresh = np.percentile(pks, 100*(1-doClip/len(pks)))
    
    ix = np.argmin(np.abs(xi-thresh))
    falsePosRate = csmodel2[ix]/csf2[ix]
    detectionRate = (csf2[ix]-csmodel2[ix])/np.max(csf2-csmodel2)

    return thresh, falsePosRate, detectionRate, low_spk
Ejemplo n.º 49
0
def maxent(A, b, lamb, w=None, x0=None):
    assert isinstance(
        A, ndarray) and A.ndim == 2, 'A is expected to be a 2-D Numpy array'

    assert isinstance(b, ndarray), 'b is expected to be a 1-D Numpy array'
    b = b.squeeze()
    assert b.ndim <= 1, 'b is expected to be a 1-D Numpy array'

    lamb = atleast_1d(lamb)
    assert lamb.ndim == 1 and lamb.size >= 0, 'lamb must be a 1-D vector or scalar'
    assert (lamb >= 0).all(), 'Regularization parameter lamb must be positive'

    #%% Set defaults.
    flat = 1e-3  # Measures a flat minimum.
    flatrange = 10  # How many iterations before a minimum is considered flat.
    maxit = 150  # Maximum number of CG iterations.
    minstep = 1e-12  # Determines the accuracy of x_lambda.
    sigma = 0.5  # Threshold used in descent test.
    tau0 = 1e-3  # Initial threshold used in secant root finder.

    #%% Initialization.
    m, n = A.shape
    lamb = atleast_1d(lamb)
    Nlambda = lamb.size

    x_lambda = zeros((n, Nlambda), order='F')
    F = zeros(maxit)

    if w is None:
        w = ones(n, dtype=float)  #needs to be column vector

    if x0 is None:
        x0 = ones(n, dtype=float)  #needs to be column vector

    rho = empty(Nlambda, dtype=float)
    eta = empty(Nlambda, dtype=float)

    # Treat each lambda separately.
    for j in range(Nlambda):

        # Prepare for nonlinear CG iteration.
        l2 = lamb[j]**2.
        x = x0
        Ax = A.dot(x)
        g = 2. * A.T.dot(Ax - b) + l2 * (1 + log(w * x))
        p = -g
        r = Ax - b

        # Start the nonlinear CG iteration here.
        delta_x = x
        dF = 1
        it = 0
        phi0 = p.T.dot(g)
        data = zeros((maxit, 3), dtype=float, order='F')
        X = zeros((n, maxit), dtype=float, order='F')

        while (norm(delta_x, 2) > minstep * norm(x, 2) and dF > flat
               and it < maxit and phi0 < 0):
            # Compute some CG quantities.
            Ap = A.dot(p)
            gamma = Ap.T.dot(Ap)
            v = A.T.dot(Ap)

            # Determine the steplength alpha by "soft" line search in which
            # the minimum of phi(alpha) = p'*g(x + alpha*p) is determined to
            # a certain "soft" tolerance.
            # First compute initial parameters for the root finder.
            alpha_left = 0
            phi_left = phi0
            if p.min() >= 0:
                alpha_right = -phi0 / (2 * gamma)
                h = 1. + alpha_right * p / x
            else:
                # Step-length control to insure a positive x + alpha*p.
                I = where(p < 0)[0]
                alpha_right = (-x[I] / p[I]).min()
                h = 1. + alpha_right * p / x
                delta = spacing(1)  #replacement for matlab eps
                while h.min() <= 0:
                    alpha_right = alpha_right * (1 - delta)
                    h = 1 + alpha_right * p / x
                    delta = delta * 2

            z = log(h)
            phi_right = phi0 + 2 * alpha_right * gamma + l2 * p.T.dot(z)
            alpha = alpha_right
            phi = phi_right

            if phi_right <= 0:  # Special treatment of the case when phi(alpha_right) = 0.
                z = log(1 + alpha * p / x)
                g_new = g + l2 * z + 2 * alpha * v
                t = g_new.T.dot(g_new)
                beta = (t - g.T.dot(g_new)) / (phi - phi0)
            else:
                # The regular case: improve the steplength alpha iteratively
                # until the new step is a descent step.
                t = 1
                u = 1
                tau = tau0
                uit = 0
                while u > -sigma * t:
                    uold = u
                    # Use the secant method to improve the root of phi(alpha) = 0
                    # to within an accuracy determined by tau.
                    phiit = 0
                    while absolute(phi / phi0) > tau:
                        phiold = phi
                        alphaold = alpha
                        alpha = (alpha_left * phi_right - alpha_right *
                                 phi_left) / (phi_right - phi_left)
                        z = log(1 + alpha * p / x)
                        phi = phi0 + 2 * alpha * gamma + l2 * p.T.dot(z)
                        if phiold == phi and alphaold == alpha and phiit > maxit:
                            warn('secant is not converging: abs(phi/phi0) = ' +
                                 str(absolute(phi / phi0)) +
                                 '  terminating phi search on iteration ' +
                                 str(phiit))
                            break
                        if phi > 0:
                            alpha_right = alpha
                            phi_right = phi
                        else:
                            alpha_left = alpha
                            phi_left = phi
                        phiit += 1
                    # To check the descent step, compute u = p'*g_new and
                    # t = norm(g_new)^2, where g_new is the gradient at x + alpha*p.
                    g_new = g + l2 * z + 2 * alpha * v
                    t = g_new.T.dot(g_new)
                    beta = (t - g.T.dot(g_new)) / (phi - phi0)
                    u = -t + beta * phi
                    if u == uold and uit > maxit:
                        warn(
                            'excessive descent iterations, terminating search on iteration '
                            + str(phiit))
                        break
                    tau = tau / 10.
                    uit += 1
            # Update the iteration vectors.
            g = g_new
            delta_x = alpha * p
            x = x + delta_x
            p = -g + beta * p
            r = r + alpha * Ap
            phi0 = p.T.dot(g)

            # Compute some norms and check for flat minimum.
            rho[j] = norm(r)
            eta[j] = x.T.dot(log(w * x))
            F[it] = rho[j]**2 + l2 * eta[j]
            if it <= flatrange:
                dF = 1.
            else:
                dF = absolute(F[it] - F[it - flatrange]) / absolute(F[it])

            data[it, ...] = array([F[it], norm(delta_x), norm(g)])
            X[..., it] = x

            it += 1

        x_lambda[..., j] = x

    return x_lambda.squeeze(), rho, eta
Ejemplo n.º 50
0
def prepare_data(mode, train_or_test):
    '''
    :param
    mode: type str, 'global' or 'once' , global用来获取全局的spk_to_idx的字典,所有说话人的列表等等
    train_or_test:type str, 'train','valid' or 'test'
     其中把每个文件夹每个人的按文件名的排序的前70%作为训练,70-80%作为valid,最后20%作为测试
    :return:
    '''
    mix_speechs = np.zeros((config.BATCH_SIZE, config.MAX_LEN))
    mix_feas = []  #应该是bs,n_frames,n_fre这么多
    mix_phase = []  #应该是bs,n_frames,n_fre这么多
    aim_fea = []  #应该是bs,n_frames,n_fre这么多
    aim_spkid = []  #np.zeros(config.BATCH_SIZE)
    aim_spkname = []  #np.zeros(config.BATCH_SIZE)
    query = []  #应该是BATCH_SIZE,shape(query)的形式,用list再转换把
    multi_spk_fea_list = []  #应该是bs个dict,每个dict里是说话人name为key,clean_fea为value的字典
    multi_spk_wav_list = []  #应该是bs个dict,每个dict里是说话人name为key,clean_fea为value的字典

    #目标数据集的总data,底下应该存放分目录的文件夹,每个文件夹应该名字是sX
    data_path = config.aim_path + '/data'
    #语音刺激
    if config.MODE == 1:
        if config.DATASET == 'WSJ0':  #开始构建数据集
            WSJ0_eval_list = [
                '440', '441', '442', '443', '444', '445', '446', '447'
            ]
            WSJ0_test_list = [
                '22g', '22h', '050', '051', '052', '053', '420', '421', '422',
                '423'
            ]
            all_spk_train = os.listdir(data_path + '/train')
            all_spk_eval = os.listdir(data_path + '/eval')
            all_spk_test = os.listdir(data_path + '/test')
            all_spk_evaltest = os.listdir(data_path + '/evaL_test')
            all_spk = all_spk_train + all_spk_eval + all_spk_test
            spk_samples_list = {}
            batch_idx = 0
            mix_k = random.randint(config.MIN_MIX, config.MAX_MIX)
            while True:
                mix_len = 0
                # mix_k=random.randint(config.MIN_MIX,config.MAX_MIX)
                if train_or_test == 'train':
                    aim_spk_k = random.sample(all_spk_train, mix_k)  #本次混合的候选人
                elif train_or_test == 'eval':
                    aim_spk_k = random.sample(all_spk_eval, mix_k)  #本次混合的候选人
                elif train_or_test == 'test':
                    aim_spk_k = random.sample(all_spk_test, mix_k)  #本次混合的候选人
                elif train_or_test == 'eval_test':
                    aim_spk_k = random.sample(all_spk_evaltest,
                                              mix_k)  #本次混合的候选人

                multi_fea_dict_this_sample = {}
                multi_wav_dict_this_sample = {}

                channel_act = None
                if 1 and config.dB and mix_k == 2:
                    dB_rate = 10**(config.dB / 20.0 * np.random.rand()
                                   )  #10**(0——0.25)
                    if np.random.rand() > 0.5:  #选择哪个通道来
                        channel_act = 1
                    else:
                        channel_act = 2
                    # print 'channel to change with dB:',channel_act,dB_rate

                if 1 and config.dB and config.MAX_MIX == 3 and mix_k == 3:
                    'DB with 3 channels.'
                    dB_rate_large = 10**(config.dB / 20.0 *
                                         (0.5 + 0.5 * np.random.rand())
                                         )  #10**(0.125——0.25)
                    dB_rate_small = 10**(config.dB / 20.0 *
                                         (0.5 * np.random.rand())
                                         )  #10**(0--0.125)
                    dB_rate_normal = 10**(config.dB / 20.0 * 0.5
                                          )  #10**(0--0.125)
                    channel_act = 'ALL'
                    # rrr=np.random.rand()
                    # if rrr>2/3.0: #选择哪个通道来
                    #     channel_act=1
                    # elif rrr<1/3.0:
                    #     channel_act=2
                    # else:
                    #     channel_act==3
                    # print 'channel to change with dB:',channel_act,dB_rate
                for k, spk in enumerate(aim_spk_k):
                    #选择dB的通道~!
                    #若是没有出现在整体列表内就注册进去,且第一次的时候读取所有的samples的名字
                    if spk not in spk_samples_list:
                        spk_samples_list[spk] = []
                        for ss in os.listdir(data_path + '/' + train_or_test +
                                             '/' + spk):
                            spk_samples_list[spk].append(ss[:-4])  #去掉.wav后缀

                        if 0:  #这部分是选择独立同分布的
                            #这个函数让spk_sanmples_list[spk]按照设定好的方式选择是train的部分还是test
                            spk_samples_list[spk] = split_forTrainDevTest(
                                spk_samples_list[spk], train_or_test)

                    #这个时候这个spk已经注册了,所以直接从里面选就好了
                    sample_name = random.sample(spk_samples_list[spk], 1)[0]
                    spk_samples_list[spk].remove(
                        sample_name)  #取出来一次之后,就把这个人去掉(避免一个batch里某段语音的重复出现)
                    spk_speech_path = data_path + '/' + train_or_test + '/' + spk + '/' + sample_name + '.wav'

                    signal, rate = sf.read(
                        spk_speech_path)  # signal 是采样值,rate 是采样频率
                    if len(signal.shape) > 1:
                        signal = signal[:, 0]
                    if rate != config.FRAME_RATE:
                        # 如果频率不是设定的频率则需要进行转换
                        signal = resampy.resample(signal,
                                                  rate,
                                                  config.FRAME_RATE,
                                                  filter='kaiser_best')
                    if signal.shape[0] > config.MAX_LEN:  # 根据最大长度裁剪
                        signal = signal[:config.MAX_LEN]
                    # 更新混叠语音长度
                    if signal.shape[0] > mix_len:
                        mix_len = signal.shape[0]

                    signal -= np.mean(signal)  # 语音信号预处理,先减去均值
                    signal /= np.max(np.abs(signal))  # 波形幅值预处理,幅值归一化

                    # 如果需要augment数据的话,先进行随机shift, 以后考虑固定shift
                    if config.AUGMENT_DATA:
                        random_shift = random.sample(range(len(signal)), 1)[0]
                        signal = signal[random_shift:] + signal[:random_shift]

                    if signal.shape[0] < config.MAX_LEN:  # 根据最大长度用 0 补齐,
                        signal = np.append(
                            signal, np.zeros(config.MAX_LEN - signal.shape[0]))

                    if k == 0:  #第一个作为目标
                        aim_spkname.append(aim_spk_k[0])
                        # aim_spk=eval(re.findall('\d+',aim_spk_k[0])[0])-1 #选定第一个作为目标说话人
                        #TODO:这里有个问题是spk是从1开始的貌似,这个后面要统一一下 --> 已经解决,构建了spk和idx的双向索引
                        aim_spk_speech = signal
                        aim_spkid.append(aim_spkname)
                        if mix_k == 2 and channel_act == 1:
                            signal = signal * dB_rate
                            print 'channel 1 for 2db:', dB_rate
                        if mix_k == 3:
                            signal = signal * dB_rate_normal
                            print 'channel 1(normal):', dB_rate_normal
                        wav_mix = signal
                        aim_fea_clean = np.transpose(
                            np.abs(
                                librosa.core.spectrum.stft(
                                    signal, config.FRAME_LENGTH,
                                    config.FRAME_SHIFT)))
                        aim_fea.append(aim_fea_clean)
                        # 把第一个人顺便也注册进去混合dict里
                        multi_fea_dict_this_sample[spk] = aim_fea_clean
                        multi_wav_dict_this_sample[spk] = signal

                        #视频处理部分,为了得到query
                    else:
                        if mix_k == 2 and channel_act == 2:
                            signal = signal * dB_rate
                            print 'channel 2 for 2db:', dB_rate

                        if mix_k == 3 and k == 1:
                            signal = signal * dB_rate_large
                            print 'channel 2(large):', dB_rate_large
                        if mix_k == 3 and k == 2:
                            signal = signal * dB_rate_small
                            print 'channel 3(small):', dB_rate_small

                        wav_mix = wav_mix + signal  # 混叠后的语音
                        # 这个说话人的语音
                        some_fea_clean = np.transpose(
                            np.abs(
                                librosa.core.spectrum.stft(
                                    signal,
                                    config.FRAME_LENGTH,
                                    config.FRAME_SHIFT,
                                )))
                        multi_fea_dict_this_sample[spk] = some_fea_clean
                        multi_wav_dict_this_sample[spk] = signal

                multi_spk_fea_list.append(
                    multi_fea_dict_this_sample)  #把这个sample的dict传进去
                multi_spk_wav_list.append(
                    multi_wav_dict_this_sample)  #把这个sample的dict传进去

                # 这里采用log 以后可以考虑采用MFCC或GFCC特征做为输入
                if config.IS_LOG_SPECTRAL:
                    feature_mix = np.log(
                        np.transpose(
                            np.abs(
                                librosa.core.spectrum.stft(
                                    wav_mix,
                                    config.FRAME_LENGTH,
                                    config.FRAME_SHIFT,
                                    window=config.WINDOWS))) + np.spacing(1))
                else:
                    feature_mix = np.transpose(
                        np.abs(
                            librosa.core.spectrum.stft(
                                wav_mix,
                                config.FRAME_LENGTH,
                                config.FRAME_SHIFT,
                            )))

                mix_speechs[batch_idx, :] = wav_mix
                mix_feas.append(feature_mix)
                mix_phase.append(
                    np.transpose(
                        librosa.core.spectrum.stft(
                            wav_mix,
                            config.FRAME_LENGTH,
                            config.FRAME_SHIFT,
                        )))
                batch_idx += 1
                print 'batch_dix:{}/{}'.format(batch_idx, config.BATCH_SIZE)
                if batch_idx == config.BATCH_SIZE:  #填满了一个batch
                    mix_feas = np.array(mix_feas)
                    mix_phase = np.array(mix_phase)
                    aim_fea = np.array(aim_fea)
                    # aim_spkid=np.array(aim_spkid)
                    query = np.array(query)
                    print '\nspk_list_from_this_gen:{}'.format(aim_spkname)
                    print 'aim spk list:', [
                        one.keys() for one in multi_spk_fea_list
                    ]
                    # print '\nmix_speechs.shape,mix_feas.shape,aim_fea.shape,aim_spkname.shape,query.shape,all_spk_num:'
                    # print mix_speechs.shape,mix_feas.shape,aim_fea.shape,len(aim_spkname),query.shape,len(all_spk)
                    if mode == 'global':
                        all_spk = sorted(all_spk)
                        all_spk = sorted(all_spk_train)
                        all_spk_eval = sorted(all_spk_eval)
                        all_spk_test = sorted(all_spk_test)
                        dict_spk_to_idx = {
                            spk: idx
                            for idx, spk in enumerate(all_spk)
                        }
                        dict_idx_to_spk = {
                            idx: spk
                            for idx, spk in enumerate(all_spk)
                        }
                        yield all_spk,dict_spk_to_idx,dict_idx_to_spk,\
                              aim_fea.shape[1],aim_fea.shape[2],32,len(all_spk)
                        #上面的是:语音长度、语音频率、视频分割多少帧 TODO:后面把这个替换了query.shape[1]
                    elif mode == 'once':
                        yield {
                            'mix_wav': mix_speechs,
                            'mix_feas': mix_feas,
                            'mix_phase': mix_phase,
                            'aim_fea': aim_fea,
                            'aim_spkname': aim_spkname,
                            'query': query,
                            'num_all_spk': len(all_spk),
                            'multi_spk_fea_list': multi_spk_fea_list,
                            'multi_spk_wav_list': multi_spk_wav_list
                        }

                    batch_idx = 0
                    mix_speechs = np.zeros((config.BATCH_SIZE, config.MAX_LEN))
                    mix_feas = []  #应该是bs,n_frames,n_fre这么多
                    mix_phase = []
                    aim_fea = []  #应该是bs,n_frames,n_fre这么多
                    aim_spkid = []  #np.zeros(config.BATCH_SIZE)
                    aim_spkname = []
                    query = []  #应该是BATCH_SIZE,shape(query)的形式,用list再转换把
                    multi_spk_fea_list = []
                    multi_spk_wav_list = []

        else:
            raise ValueError('No such dataset:{} for Speech.'.format(
                config.DATASET))

        pass

    #图像刺激
    elif config.MODE == 2:
        pass

    #视频刺激
    elif config.MODE == 3:
        if config.DATASET == 'AVA':
            pass
        elif config.DATASET == 'GRID':
            #开始构建GRID数据集
            all_spk = os.listdir(data_path)
            spk_samples_list = {}
            batch_idx = 0
            while True:
                mix_len = 0
                mix_k = random.randint(config.MIN_MIX, config.MAX_MIX)
                aim_spk_k = random.sample(all_spk, mix_k)  #本次混合的候选人
                multi_fea_dict_this_sample = {}
                multi_wav_dict_this_sample = {}

                for k, spk in enumerate(aim_spk_k):
                    #若是没有出现在整体列表内就注册进去,且第一次的时候读取所有的samples的名字
                    if spk not in spk_samples_list:
                        spk_samples_list[spk] = []
                        for ss in os.listdir(data_path + '/' + spk + '/' +
                                             spk + '_speech'):
                            spk_samples_list[spk].append(ss[:-4])  #去掉.wav后缀

                        #这个函数让spk_sanmples_list[spk]按照设定好的方式选择是train的部分还是test
                        spk_samples_list[spk] = split_forTrainDevTest(
                            spk_samples_list[spk], train_or_test)

                    #这个时候这个spk已经注册了,所以直接从里面选就好了
                    sample_name = random.sample(spk_samples_list[spk], 1)[0]
                    spk_samples_list[spk].remove(
                        sample_name)  #取出来一次之后,就把这个人去掉(避免一个batch里某段语音的重复出现)
                    spk_speech_path = data_path + '/' + spk + '/' + spk + '_speech/' + sample_name + '.wav'

                    signal, rate = sf.read(
                        spk_speech_path)  # signal 是采样值,rate 是采样频率
                    if len(signal.shape) > 1:
                        signal = signal[:, 0]
                    if rate != config.FRAME_RATE:
                        # 如果频率不是设定的频率则需要进行转换
                        signal = resampy.resample(signal,
                                                  rate,
                                                  config.FRAME_RATE,
                                                  filter='kaiser_best')
                    if signal.shape[0] > config.MAX_LEN:  # 根据最大长度裁剪
                        signal = signal[:config.MAX_LEN]
                    # 更新混叠语音长度
                    if signal.shape[0] > mix_len:
                        mix_len = signal.shape[0]

                    signal -= np.mean(signal)  # 语音信号预处理,先减去均值
                    signal /= np.max(np.abs(signal))  # 波形幅值预处理,幅值归一化

                    # 如果需要augment数据的话,先进行随机shift, 以后考虑固定shift
                    if config.AUGMENT_DATA:
                        random_shift = random.sample(range(len(signal)), 1)[0]
                        signal = signal[random_shift:] + signal[:random_shift]

                    if signal.shape[0] < config.MAX_LEN:  # 根据最大长度用 0 补齐,
                        signal = np.append(
                            signal, np.zeros(config.MAX_LEN - signal.shape[0]))

                    if k == 0:  #第一个作为目标
                        aim_spkname.append(aim_spk_k[0])
                        # aim_spk=eval(re.findall('\d+',aim_spk_k[0])[0])-1 #选定第一个作为目标说话人
                        #TODO:这里有个问题是spk是从1开始的貌似,这个后面要统一一下 --> 已经解决,构建了spk和idx的双向索引
                        aim_spk_speech = signal
                        aim_spkid.append(aim_spkname)
                        wav_mix = signal
                        aim_fea_clean = np.transpose(
                            np.abs(
                                librosa.core.spectrum.stft(
                                    signal, config.FRAME_LENGTH,
                                    config.FRAME_SHIFT)))
                        aim_fea.append(aim_fea_clean)
                        # 把第一个人顺便也注册进去混合dict里
                        multi_fea_dict_this_sample[spk] = aim_fea_clean
                        multi_wav_dict_this_sample[spk] = signal

                    else:
                        wav_mix = wav_mix + signal  # 混叠后的语音
                        # 这个说话人的语音
                        some_fea_clean = np.transpose(
                            np.abs(
                                librosa.core.spectrum.stft(
                                    signal,
                                    config.FRAME_LENGTH,
                                    config.FRAME_SHIFT,
                                )))
                        multi_fea_dict_this_sample[spk] = some_fea_clean
                        multi_wav_dict_this_sample[spk] = signal

                multi_spk_fea_list.append(
                    multi_fea_dict_this_sample)  #把这个sample的dict传进去
                multi_spk_wav_list.append(
                    multi_wav_dict_this_sample)  #把这个sample的dict传进去

                # 这里采用log 以后可以考虑采用MFCC或GFCC特征做为输入
                if config.IS_LOG_SPECTRAL:
                    feature_mix = np.log(
                        np.transpose(
                            np.abs(
                                librosa.core.spectrum.stft(
                                    wav_mix,
                                    config.FRAME_LENGTH,
                                    config.FRAME_SHIFT,
                                    window=config.WINDOWS))) + np.spacing(1))
                else:
                    feature_mix = np.transpose(
                        np.abs(
                            librosa.core.spectrum.stft(
                                wav_mix,
                                config.FRAME_LENGTH,
                                config.FRAME_SHIFT,
                            )))

                mix_speechs[batch_idx, :] = wav_mix
                mix_feas.append(feature_mix)
                mix_phase.append(
                    np.transpose(
                        librosa.core.spectrum.stft(
                            wav_mix,
                            config.FRAME_LENGTH,
                            config.FRAME_SHIFT,
                        )))
                batch_idx += 1
                print 'batch_dix:{}/{},'.format(batch_idx, config.BATCH_SIZE),
                if batch_idx == config.BATCH_SIZE:  #填满了一个batch
                    mix_feas = np.array(mix_feas)
                    mix_phase = np.array(mix_phase)
                    aim_fea = np.array(aim_fea)
                    # aim_spkid=np.array(aim_spkid)
                    query = np.array(query)
                    print '\nspk_list_from_this_gen:{}'.format(aim_spkname)
                    print 'aim spk list:', [
                        one.keys() for one in multi_spk_fea_list
                    ]
                    # print '\nmix_speechs.shape,mix_feas.shape,aim_fea.shape,aim_spkname.shape,query.shape,all_spk_num:'
                    # print mix_speechs.shape,mix_feas.shape,aim_fea.shape,len(aim_spkname),query.shape,len(all_spk)
                    if mode == 'global':
                        all_spk = sorted(all_spk)
                        dict_spk_to_idx = {
                            spk: idx
                            for idx, spk in enumerate(all_spk)
                        }
                        dict_idx_to_spk = {
                            idx: spk
                            for idx, spk in enumerate(all_spk)
                        }
                        yield all_spk,dict_spk_to_idx,dict_idx_to_spk,\
                              aim_fea.shape[1],aim_fea.shape[2],32,len(all_spk)
                        #上面的是:语音长度、语音频率、视频分割多少帧 TODO:后面把这个替换了query.shape[1]
                    elif mode == 'once':
                        yield {
                            'mix_wav': mix_speechs,
                            'mix_feas': mix_feas,
                            'mix_phase': mix_phase,
                            'aim_fea': aim_fea,
                            'aim_spkname': aim_spkname,
                            'query': query,
                            'num_all_spk': len(all_spk),
                            'multi_spk_fea_list': multi_spk_fea_list,
                            'multi_spk_wav_list': multi_spk_wav_list
                        }

                    batch_idx = 0
                    mix_speechs = np.zeros((config.BATCH_SIZE, config.MAX_LEN))
                    mix_feas = []  #应该是bs,n_frames,n_fre这么多
                    mix_phase = []
                    aim_fea = []  #应该是bs,n_frames,n_fre这么多
                    aim_spkid = []  #np.zeros(config.BATCH_SIZE)
                    aim_spkname = []
                    query = []  #应该是BATCH_SIZE,shape(query)的形式,用list再转换把
                    multi_spk_fea_list = []
                    multi_spk_wav_list = []

        else:
            raise ValueError('No such dataset:{} for Video'.format(
                config.DATASET))

    #概念刺激
    elif config.MODE == 4:
        pass

    else:
        raise ValueError('No such Model:{}'.format(config.MODE))
Ejemplo n.º 51
0
    def solve(self,
              problem,
              x=None,
              mininner=1,
              maxinner=None,
              Delta_bar=None,
              Delta0=None):
        man = problem.manifold
        verbosity = problem.verbosity

        if maxinner is None:
            maxinner = man.dim

        # Set default Delta_bar and Delta0 separately to deal with additional
        # logic: if Delta_bar is provided but not Delta0, let Delta0
        # automatically be some fraction of the provided Delta_bar.
        if Delta_bar is None:
            try:
                Delta_bar = man.typicaldist
            except NotImplementedError:
                Delta_bar = np.sqrt(man.dim)
        if Delta0 is None:
            Delta0 = Delta_bar / 8

        cost = problem.cost
        grad = problem.grad
        hess = problem.hess

        # If no starting point is specified, generate one at random.
        if x is None:
            x = man.rand()

        # Initializations
        time0 = time.time()

        # k counts the outer (TR) iterations. The semantic is that k counts the
        # number of iterations fully executed so far.
        k = 0

        # Initialize solution and companion measures: f(x), fgrad(x)
        fx = cost(x)
        fgradx = grad(x)
        norm_grad = man.norm(x, fgradx)

        # Initialize the trust region radius
        Delta = Delta0

        # To keep track of consecutive radius changes, so that we can warn the
        # user if it appears necessary.
        consecutive_TRplus = 0
        consecutive_TRminus = 0

        # ** Display:
        if verbosity >= 1:
            print("Optimizing...")
        if verbosity >= 2:
            print("{:44s}f: {:+.6e}   |grad|: {:.6e}".format(
                " ", float(fx), norm_grad))

        self._start_optlog(extraiterfields=['gradnorm'])

        while True:
            # *************************
            # ** Begin TR Subproblem **
            # *************************

            if self._logverbosity >= 2:
                self._append_optlog(k + 1, x, fx, gradnorm=norm_grad)

            # Determine eta0
            if not self.use_rand:
                # Pick the zero vector
                eta = man.zerovec(x)
            else:
                # Random vector in T_x M (this has to be very small)
                eta = 1e-6 * man.randvec(x)
                # Must be inside trust region
                while man.norm(x, eta) > Delta:
                    eta = np.sqrt(np.sqrt(np.spacing(1)))

            # Solve TR subproblem approximately
            eta, Heta, numit, stop_inner = self._truncated_conjugate_gradient(
                problem, x, fgradx, eta, Delta, self.theta, self.kappa,
                mininner, maxinner)

            srstr = self.TCG_STOP_REASONS[stop_inner]

            # If using randomized approach, compare result with the Cauchy
            # point. Convergence proofs assume that we achieve at least (a
            # fraction of) the reduction of the Cauchy point. After this
            # if-block, either all eta-related quantities have been changed
            # consistently, or none of them have.

            if self.use_rand:
                used_cauchy = False
                # Check the curvature
                Hg = hess(x, fgradx)
                g_Hg = man.inner(x, fgradx, Hg)
                if g_Hg <= 0:
                    tau_c = 1
                else:
                    tau_c = min(norm_grad**3 / (Delta * g_Hg), 1)

                # and generate the Cauchy point.
                eta_c = -tau_c * Delta / norm_grad * fgradx
                Heta_c = -tau_c * Delta / norm_grad * Hg

                # Now that we have computed the Cauchy point in addition to the
                # returned eta, we might as well keep the best of them.
                mdle = (fx + man.inner(x, fgradx, eta) +
                        0.5 * man.inner(x, Heta, eta))
                mdlec = (fx + man.inner(x, fgradx, eta_c) +
                         0.5 * man.inner(x, Heta_c, eta_c))
                if mdlec < mdle:
                    eta = eta_c
                    Heta = Heta_c
                    used_cauchy = True

            # This is only computed for logging purposes, because it may be
            # useful for some user-defined stopping criteria. If this is not
            # cheap for specific applications (compared to evaluating the
            # cost), we should reconsider this.
            # norm_eta = man.norm(x, eta)

            # Compute the tentative next iterate (the proposal)
            x_prop = man.retr(x, eta)

            # Compute the function value of the proposal
            fx_prop = cost(x_prop)

            # Will we accept the proposal or not? Check the performance of the
            # quadratic model against the actual cost.
            rhonum = fx - fx_prop
            rhoden = -man.inner(x, fgradx, eta) - 0.5 * man.inner(x, eta, Heta)

            # rhonum could be anything.
            # rhoden should be nonnegative, as guaranteed by tCG, baring
            # numerical errors.

            # Heuristic -- added Dec. 2, 2013 (NB) to replace the former
            # heuristic. This heuristic is documented in the book by Conn Gould
            # and Toint on trust-region methods, section 17.4.2. rhonum
            # measures the difference between two numbers. Close to
            # convergence, these two numbers are very close to each other, so
            # that computing their difference is numerically challenging: there
            # may be a significant loss in accuracy. Since the acceptance or
            # rejection of the step is conditioned on the ratio between rhonum
            # and rhoden, large errors in rhonum result in a very large error
            # in rho, hence in erratic acceptance / rejection. Meanwhile, close
            # to convergence, steps are usually trustworthy and we should
            # transition to a Newton- like method, with rho=1 consistently. The
            # heuristic thus shifts both rhonum and rhoden by a small amount
            # such that far from convergence, the shift is irrelevant and close
            # to convergence, the ratio rho goes to 1, effectively promoting
            # acceptance of the step.  The rationale is that close to
            # convergence, both rhonum and rhoden are quadratic in the distance
            # between x and x_prop. Thus, when this distance is on the order of
            # sqrt(eps), the value of rhonum and rhoden is on the order of eps,
            # which is indistinguishable from the numerical error, resulting in
            # badly estimated rho's.
            # For abs(fx) < 1, this heuristic is invariant under offsets of f
            # but not under scaling of f. For abs(fx) > 1, the opposite holds.
            # This should not alarm us, as this heuristic only triggers at the
            # very last iterations if very fine convergence is demanded.
            rho_reg = max(1, abs(fx)) * np.spacing(1) * self.rho_regularization
            rhonum = rhonum + rho_reg
            rhoden = rhoden + rho_reg

            # This is always true if a linear, symmetric operator is used for
            # the Hessian (approximation) and if we had infinite numerical
            # precision.  In practice, nonlinear approximations of the Hessian
            # such as the built-in finite difference approximation and finite
            # numerical accuracy can cause the model to increase. In such
            # scenarios, we decide to force a rejection of the step and a
            # reduction of the trust-region radius. We test the sign of the
            # regularized rhoden since the regularization is supposed to
            # capture the accuracy to which rhoden is computed: if rhoden were
            # negative before regularization but not after, that should not be
            # (and is not) detected as a failure.
            #
            # Note (Feb. 17, 2015, NB): the most recent version of tCG already
            # includes a mechanism to ensure model decrease if the Cauchy step
            # attained a decrease (which is theoretically the case under very
            # lax assumptions). This being said, it is always possible that
            # numerical errors will prevent this, so that it is good to keep a
            # safeguard.
            #
            # The current strategy is that, if this should happen, then we
            # reject the step and reduce the trust region radius. This also
            # ensures that the actual cost values are monotonically decreasing.
            model_decreased = (rhoden >= 0)

            if not model_decreased:
                srstr = srstr + ", model did not decrease"

            try:
                rho = rhonum / rhoden
            except ZeroDivisionError:
                # Added June 30, 2015 following observation by BM.  With this
                # modification, it is guaranteed that a step rejection is
                # always accompanied by a TR reduction. This prevents
                # stagnation in this "corner case" (NaN's really aren't
                # supposed to occur, but it's nice if we can handle them
                # nonetheless).
                print("rho is NaN! Forcing a radius decrease. This should "
                      "not happen.")
                rho = np.nan

            # Choose the new TR radius based on the model performance
            trstr = "   "
            # If the actual decrease is smaller than 1/4 of the predicted
            # decrease, then reduce the TR radius.
            if rho < 1.0 / 4 or not model_decreased or np.isnan(rho):
                trstr = "TR-"
                Delta = Delta / 4
                consecutive_TRplus = 0
                consecutive_TRminus = consecutive_TRminus + 1
                if consecutive_TRminus >= 5 and verbosity >= 1:
                    consecutive_TRminus = -np.inf
                    print(" +++ Detected many consecutive TR- (radius "
                          "decreases).")
                    print(" +++ Consider decreasing options.Delta_bar "
                          "by an order of magnitude.")
                    print(" +++ Current values: Delta_bar = {:g} and "
                          "Delta0 = {:g}".format(Delta_bar, Delta0))
            # If the actual decrease is at least 3/4 of the precicted decrease
            # and the tCG (inner solve) hit the TR boundary, increase the TR
            # radius. We also keep track of the number of consecutive
            # trust-region radius increases. If there are many, this may
            # indicate the need to adapt the initial and maximum radii.
            elif rho > 3.0 / 4 and (stop_inner == self.NEGATIVE_CURVATURE
                                    or stop_inner == self.EXCEEDED_TR):
                trstr = "TR+"
                Delta = min(2 * Delta, Delta_bar)
                consecutive_TRminus = 0
                consecutive_TRplus = consecutive_TRplus + 1
                if consecutive_TRplus >= 5 and verbosity >= 1:
                    consecutive_TRplus = -np.inf
                    print(" +++ Detected many consecutive TR+ (radius "
                          "increases).")
                    print(" +++ Consider increasing options.Delta_bar "
                          "by an order of magnitude.")
                    print(" +++ Current values: Delta_bar = {:g} and "
                          "Delta0 = {:g}.".format(Delta_bar, Delta0))
            else:
                # Otherwise, keep the TR radius constant.
                consecutive_TRplus = 0
                consecutive_TRminus = 0

            # Choose to accept or reject the proposed step based on the model
            # performance. Note the strict inequality.
            if model_decreased and rho > self.rho_prime:
                # accept = True
                accstr = "acc"
                x = x_prop
                fx = fx_prop
                fgradx = grad(x)
                norm_grad = man.norm(x, fgradx)
            else:
                # accept = False
                accstr = "REJ"

            # k is the number of iterations we have accomplished.
            k = k + 1

            # ** Display:
            if verbosity == 2:
                print("{:.3s} {:.3s}   k: {:5d}     num_inner: "
                      "{:5d}     f: {:+e}   |grad|: {:e}   "
                      "{:s}".format(accstr, trstr, k, numit, float(fx),
                                    norm_grad, srstr))
            elif verbosity > 2:
                if self.use_rand and used_cauchy:
                    print("USED CAUCHY POINT")
                print("{:.3s} {:.3s}    k: {:5d}     num_inner: "
                      "{:5d}     {:s}".format(accstr, trstr, k, numit, srstr))
                print("       f(x) : {:+e}     |grad| : "
                      "{:e}".format(fx, norm_grad))
                print("        rho : {:e}".format(rho))

            # ** CHECK STOPPING criteria
            stop_reason = self._check_stopping_criterion(time0,
                                                         gradnorm=norm_grad,
                                                         iter=k)

            if stop_reason:
                if verbosity >= 1:
                    print(stop_reason)
                    print('')
                break

        if self._logverbosity <= 0:
            return x
        else:
            self._stop_optlog(x,
                              fx,
                              stop_reason,
                              time0,
                              gradnorm=norm_grad,
                              iter=k)
            return x, self._optlog
Ejemplo n.º 52
0
def solve_nd_trust_region_subproblem(
        B: np.ndarray,
        g: np.ndarray,
        delta: float,
        logger: Union[logging.Logger, None] = None) -> Tuple[np.ndarray, str]:
    r"""
    This function exactly solves the n-dimensional subproblem.

    :math:`argmin_s\{s^T B s + s^T g = 0: ||s|| <= \Delta, s \in \mathbb{
    R}^n\}`

    The  solution to is characterized by the equation
    :math:`-(B + \lambda I)s = g`. If B is positive definite, the solution can
    be obtained by :math:`\lambda = 0`$` if :math:`Bs = -g` satisfies
    :math:`||s|| <= \Delta`. If B is indefinite or :math:`Bs = -g`
    satisfies :math:`||s|| > \Delta` and an approppriate :math:`\lambda` has
    to be  identified via 1D rootfinding of the secular equation

    :math:`\phi(\lambda) = \frac{1}{||s(\lambda)||} - \frac{1}{\Delta} = 0`

    with :math:`s(\lambda)` computed according to an eigenvalue decomposition
    of B. The eigenvalue decomposition, although being more expensive than a
    cholesky decomposition, has the advantage that eigenvectors are
    invariant to changes in :math:`\lambda` and eigenvalues are linear in
    :math:`\lambda`, so factorization only has to be performed once. We perform
    the linesearch via Newton's algorithm and Brent-Q as fallback.
    The hard case is treated seperately and serves as general fallback.

    :param B:
        Hessian of the quadratic subproblem
    :param g:
        Gradient of the quadratic subproblem
    :param delta:
        Norm boundary for the solution of the quadratic subproblem
    :param logger:
        Logger instance to be used for logging

    :return:
        s: Selected step,
        step_type: Type of solution that was obtained

    """
    if logger is None:
        logger = logging.getLogger('fides')

    if delta == 0:
        return np.zeros(g.shape), 'zero'

    # See Nocedal & Wright 2006 for details
    # INITIALIZATION

    # instead of a cholesky factorization, we go with an eigenvalue
    # decomposition, which works pretty well for n=2
    eigvals, eigvecs = linalg.eig(B)
    eigvals = np.real(eigvals)
    eigvecs = np.real(eigvecs)
    w = -eigvecs.T.dot(g)
    jmin = eigvals.argmin()
    mineig = eigvals[jmin]

    # since B symmetric eigenvecs V are orthonormal
    # B + lambda I = V * (E + lambda I) * V.T
    # inv(B + lambda I) = V * inv(E + lambda I) * V.T
    # w = V.T * g
    # s(lam) = V * w./(eigvals + lam)
    # ds(lam) = - V * w./((eigvals + lam)**2)
    # \phi(lam) = 1/||s(lam)|| - 1/delta
    # \phi'(lam) = - s(lam).T*ds(lam)/||s(lam)||^3

    # POSITIVE DEFINITE
    if mineig > 0:  # positive definite
        s = np.real(slam(0, w, eigvals, eigvecs))  # s = - self.cB\self.cg_hat
        if norm(s) <= delta + np.sqrt(np.spacing(1)):  # CASE 0
            logger.debug('Interior subproblem solution')
            return s, 'posdef'
        else:
            laminit = 0
    else:
        laminit = -mineig

    # INDEFINITE CASE
    # note that this includes what Nocedal calls the "hard case" but with
    # ||s|| > delta, so the provided formula is not applicable,
    # the respective w should be close to 0 anyways
    if secular(laminit, w, eigvals, eigvecs, delta) < 0:
        maxiter = 100
        try:
            r = newton(
                secular,
                laminit,
                dsecular,
                tol=1e-12,
                maxiter=maxiter,
                args=(w, eigvals, eigvecs, delta),
            )
            s = slam(r, w, eigvals, eigvecs)
            if norm(s) <= delta + 1e-12:
                logger.debug('Found boundary subproblem solution via newton')
                return s, 'indef'
        except RuntimeError:
            pass
        try:
            xa = laminit
            xb = (laminit + np.sqrt(np.spacing(1))) * 10
            # search to the right for a change of sign
            while secular(xb, w, eigvals, eigvecs, delta) < 0 and \
                    maxiter > 0:
                xa = xb
                xb = xb * 10
                maxiter -= 1
            if maxiter > 0:
                r = brentq(secular,
                           xa,
                           xb,
                           xtol=1e-12,
                           maxiter=maxiter,
                           args=(w, eigvals, eigvecs, delta))
                s = slam(r, w, eigvals, eigvecs)
                if norm(s) <= delta + np.sqrt(np.spacing(1)):
                    logger.debug(
                        'Found boundary subproblem solution via brentq')
                    return s, 'indef'
        except RuntimeError:
            pass  # may end up here due to ill-conditioning, treat as hard case

    # HARD CASE (gradient is orthogonal to eigenvector to smallest eigenvalue)
    w[(eigvals - mineig) == 0] = 0
    s = slam(-mineig, w, eigvals, eigvecs)
    # we know that ||s(lam) + sigma*v_jmin|| = delta, since v_jmin is
    # orthonormal, we can just substract the difference in norm to get
    # the right length.

    sigma = np.sqrt(max(delta**2 - norm(s)**2, 0))
    s = s + sigma * eigvecs[:, jmin]
    logger.debug('Found boundary 2D subproblem solution via hard case')
    return s, 'hard'
Ejemplo n.º 53
0
 def s_plm(cls, x, c):
     S2 = np.linalg.inv(c.T @ c) @ c.T @ x  # Multilinear regression
     S2[S2 <= np.spacing(1)] = np.spacing(1)  # avoid 0 values
     s = S2 / (np.sum(S2, axis=1) * np.ones(
         (np.size(S2, axis=1), 1))).T  # Normalization
     return s
Ejemplo n.º 54
0
def get_mask(i, W, H):
    eps = np.spacing(1)
    V = np.dot(W,H)
    V1 = np.dot(W[:,i].reshape(-1,1),H[i,:].reshape(1,-1))
    return V1/(V+eps)
Ejemplo n.º 55
0
def test_ufunc_spacing_u(A: dace.uint32[10]):
    return np.spacing(A)
Ejemplo n.º 56
0
def main():
    """Main function of the feature_extractor script.

    Loading the audio files and extracting the mel band features.
    """
    with open(path.join('feature_params.yml'), 'r') as f:
        feature_params = yaml.load(f)

    audio_file_dir = feature_params['general']['audio_files_dir']

    feature_set_dir = feature_params['general']['feature_set_dir']

    if not os.path.exists(path.join(feature_set_dir)):
        os.makedirs(path.join(feature_set_dir))

    # defining the initial parameters for extracting the features
    smpl_rate = feature_params['feature']['sampling_rate']
    n_fft_ = feature_params['feature']['n_fft']
    win_length = feature_params['feature']['win_length']
    hop_length = feature_params['feature']['hop_length']
    n_mel_bands = feature_params['feature']['n_mel_bands']
    f_min = feature_params['feature']['f_min']
    f_max = feature_params['feature']['f_max']
    htk_ = feature_params['feature']['htk_']
    spectrogram_type = feature_params['feature']['spectrogram_type']
    window_name = feature_params['feature']['window_name']
    symmetric = feature_params['feature']['symmetric']

    if window_name in scipy.signal.windows.__all__:
        win_func = eval(window_name)
    else:
        raise ValueError('Unknown window function')

    window = win_func(win_length, sym=symmetric)

    mel_basis = librosa.filters.mel(sr=smpl_rate,
                                    n_fft=n_fft_,
                                    n_mels=n_mel_bands,
                                    fmin=f_min,
                                    fmax=f_max,
                                    htk=htk_)

    audio_data_path = glob.glob(os.path.join(audio_file_dir, '*.wav'))

    eps = np.spacing(1)

    for ind, smpl in enumerate(audio_data_path):

        wav_smpl, sr_ = sf.read(smpl, always_2d=True)

        # to converts wav_smpl[samples, channels] to wav_smpl[channels, samples]
        wav_smpl = wav_smpl.T

        feature_matrix_container = []
        stat_container = []

        for channel in range(wav_smpl.shape[0]):
            spectrogram_ = spectrogram(y=wav_smpl[channel, :],
                                       n_fft=n_fft_,
                                       win_length=win_length,
                                       hop_length=hop_length,
                                       spectrogram_type=spectrogram_type,
                                       center=True,
                                       window=window)

            feature_matrix = np.dot(mel_basis, spectrogram_)

            feature_matrix = np.log(feature_matrix + eps)

            feature_matrix_container.append(feature_matrix)

            stat_container.append({
                'mean': np.mean(feature_matrix.T, axis=0),
                'std': np.std(feature_matrix.T, axis=0)
            })

        with open(
                path.join(feature_set_dir,
                          smpl.split('/')[-1].replace('.wav', '.p')),
                'wb') as f_name:
            pickle.dump(
                {
                    'feat': feature_matrix_container,
                    'stat': stat_container
                },
                f_name,
                protocol=2)

    with open(path.join(feature_set_dir, 'feature_set_params.yaml'), 'w') as f:
        yaml.dump(
            {
                'sample rate': smpl_rate,
                'minimum frequency': f_min,
                'maximum frequency': f_max,
                'window': window_name,
                'window length': win_length,
                'hop length': hop_length,
                'fft length': n_fft_,
                'mel bands': n_mel_bands,
                'spectrogram type': spectrogram_type,
                'htk': htk_
            },
            f,
            default_flow_style=True)
Ejemplo n.º 57
0
def test_ufunc_spacing_f(A: dace.float32[10]):
    return np.spacing(A)
Ejemplo n.º 58
0
def t1_mp2rage(inv1m_arr=None,
               inv1p_arr=None,
               inv2m_arr=None,
               inv2p_arr=None,
               rho_arr=None,
               regularization=np.spacing(1),
               eff_arr=None,
               t1_value_range=(100, 5000),
               t1_num=512,
               eff_num=32,
               **acq_param_kws):
    """
    Calculate the T1 map from an MP2RAGE acquisition.

    Args:
        inv1m_arr (float|np.ndarray): Magnitude of the first inversion image.
        inv1p_arr (float|np.ndarray): Phase of the first inversion image.
        inv2m_arr (float|np.ndarray): Magnitude of the second inversion image.
        inv2p_arr (float|np.ndarray): Phase of the second inversion image.
        eff_arr (float|np.array|None): Efficiency of the RF pulse excitation.
            This is equivalent to the normalized B1T field.
            Note that this must have the same spatial dimensions as the images
            acquired with MP2RAGE.
            If None, no correction for the RF efficiency is performed.
        t1_value_range (tuple[float]): The T1 value range to consider.
            The format is (min, max) where min < max.
            Values should be positive.
        t1_num (int): The base number of sampling points of T1.
            The actual number of sampling points is usually smaller, because of
            the removal of non-bijective branches.
            This affects the precision of the MP2RAGE estimation.
        eff_num (int): The base number of sampling points for the RF efficiency.
            This affects the precision of the RF efficiency correction.
        **acq_param_kws (dict): The acquisition parameters.
            This should match the signature of:  `mp2rage.acq_to_seq_params`.

    Returns:
        t1_arr (float|np.ndarray): The calculated T1 map for MP2RAGE.
    """
    from pymrt.sequences import mp2rage
    import matplotlib.pyplot as plt

    if eff_arr:
        # todo: implement B1T correction
        raise NotImplementedError('B1T correction is not yet implemented')
    else:
        # determine the signal expression
        t1 = np.linspace(t1_value_range[0], t1_value_range[1], t1_num)
        seq_param_kws = mp2rage.acq_to_seq_params(**acq_param_kws)[0]
        rho = mp2rage.signal(t1, **seq_param_kws)

        # plot T1 vs. RHO
        plt.figure()
        plt.plot(rho, t1)
        plt.xlabel('RHO')
        plt.ylabel('T1 (ms)')
        plt.title('T1 vs. RHO')
        plt.savefig('T1_vs_RHO.pdf', format='PDF', transparent=True)

        # remove non-bijective branches
        bijective_part = pmu.bijective_part(rho)
        t1 = t1[bijective_part]
        rho = rho[bijective_part]
        if rho[0] > rho[-1]:
            rho = rho[::-1]
            t1 = t1[::-1]

        # plot the bijective part of the graph
        plt.figure()
        plt.plot(rho, t1)
        plt.xlabel('RHO')
        plt.ylabel('T1 (ms)')
        plt.title('T1 vs. RHO (bijective part only)')
        plt.savefig('T1_vs_RHO_bij.pdf', format='PDF', transparent=True)

        # check that rho values are strictly increasing
        if not np.all(np.diff(rho) > 0):
            raise ValueError(
                'MP2RAGE look-up table was not properly prepared.')

        if rho_arr == None:
            rho_arr = uniform_mp2rage(inv1m_arr,
                                      inv1p_arr,
                                      inv2m_arr,
                                      inv2p_arr,
                                      regularization,
                                      values_interval=None)
        else:
            rho_arr = pmu.scale(rho_arr, (-0.5, 0.5), DICOM_INTERVAL)

        print(np.min(rho_arr), np.max(rho_arr))

        t1_arr = np.interp(rho_arr, rho, t1)

    return t1_arr, rho_arr
Ejemplo n.º 59
0
def get_transition_probabilities(s, x, F, a):
    """Calculate the transition probabilities for the given state and action.
    Parameters
    ----------
    s : float
        The class-independent probability of the population staying in its
        current population abundance class.
    x : int
        The population abundance class of the threatened species.
    F : int
        The time in years since last fire.
    a : int
        The action undertaken.
    Returns
    -------
    prob : array
        The transition probabilities as a vector from state (``x``, ``F``) to
        every other state given that action ``a`` is taken.
    """
    # Check that input is in range
    check_probability(s)
    check_population_class(x)
    check_fire_class(F)
    check_action(a)

    # a vector to store the transition probabilities
    prob = np.zeros(STATES)

    # the habitat suitability value
    r = get_habitat_suitability(F)
    F = transition_fire_state(F, a)

    ## Population transitions
    if x == 0:
        # population abundance class stays at 0 (extinct)
        new_state = convert_state_to_index(0, F)
        prob[new_state] = 1
    elif x == POPULATION_CLASSES - 1:
        # Population abundance class either stays at maximum or transitions
        # down
        transition_same = x
        transition_down = x - 1
        # If action 1 is taken, then the patch is burned so the population
        # abundance moves down a class.
        if a == ACTION_BURN:
            transition_same -= 1
            transition_down -= 1
        # transition probability that abundance stays the same
        new_state = convert_state_to_index(transition_same, F)
        prob[new_state] = 1 - (1 - s) * (1 - r)
        # transition probability that abundance goes down
        new_state = convert_state_to_index(transition_down, F)
        prob[new_state] = (1 - s) * (1 - r)
    else:
        # Population abundance class can stay the same, transition up, or
        # transition down.
        transition_same = x
        transition_up = x + 1
        transition_down = x - 1
        # If action 1 is taken, then the patch is burned so the population
        # abundance moves down a class.
        if a == ACTION_BURN:
            transition_same -= 1
            transition_up -= 1
            # Ensure that the abundance class doesn't go to -1
            if transition_down > 0:
                transition_down -= 1
        # transition probability that abundance stays the same
        new_state = convert_state_to_index(transition_same, F)
        prob[new_state] = s
        # transition probability that abundance goes up
        new_state = convert_state_to_index(transition_up, F)
        prob[new_state] = (1 - s) * r
        # transition probability that abundance goes down
        new_state = convert_state_to_index(transition_down, F)
        # In the case when transition_down = 0 before the effect of an action
        # is applied, then the final state is going to be the same as that for
        # transition_same, so we need to add the probabilities together.
        prob[new_state] += (1 - s) * (1 - r)

    # Make sure that the probabilities sum to one
    assert (prob.sum() - 1) < np.spacing(1)
    return prob
Ejemplo n.º 60
0
def get_mask1(i, W, H):
    eps = np.spacing(1)
    V = np.dot(W,H)
    V1 = np.dot(W[:,i],H[i,:])
    return V1/(V+eps)