def box_ciou(b1, b2): """ 输入为: ---------- b1: NDarray, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh b2: NDarray, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh 返回为: ------- ciou: NDarray, shape=(batch, feat_w, feat_h, anchor_num, 1) """ # 求出预测框左上角右下角 b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh / 2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # 求出真实框左上角右下角 b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh / 2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half # 求真实框和预测框所有的iou intersect_mins = nd.max(b1_mins, b2_mins) intersect_maxes = nd.min(b1_maxes, b2_maxes) intersect_wh = nd.max(intersect_maxes - intersect_mins, nd.zeros_like(intersect_maxes)) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] union_area = b1_area + b2_area - intersect_area iou = intersect_area / nd.clip(union_area, a_min=1e-6) # 计算中心的差距 center_distance = nd.sum(nd.power((b1_xy - b2_xy), 2), axis=-1) # 找到包裹两个框的最小框的左上角和右下角 enclose_mins = nd.min(b1_mins, b2_mins) enclose_maxes = nd.max(b1_maxes, b2_maxes) enclose_wh = nd.max(enclose_maxes - enclose_mins, nd.zeros_like(intersect_maxes)) # 计算对角线距离 enclose_diagonal = nd.sum(nd.power(enclose_wh, 2), axis=-1) ciou = iou - 1.0 * (center_distance) / nd.clip(enclose_diagonal, a_min=1e-6) v = (4 / (math.pi**2)) * nd.power( (nd.arctan(b1_wh[..., 0] / nd.clip(b1_wh[..., 1], min=1e-6)) - nd.arctan(b2_wh[..., 0] / nd.clip(b2_wh[..., 1], a_min=1e-6))), 2) alpha = v / nd.clip((1.0 - iou + v), a_max=1e-6) ciou = ciou - alpha * v return ciou
def cart2sph(self, n): temp = n[1] / n[0] if n[0] == 0: if n[1] < 0: phi = -nd.pi / 2 else: phi = nd.pi / 2 else: if n[0] > 0: phi = nd.arctan(temp) elif n[1] < 0: phi = nd.arctan(temp) - np.pi else: phi = nd.arctan(temp) + np.pi # phi = np.arctan() #arctan(y/x) theta = nd.arccos(n[2]) #arccos(z) return [phi, theta]
def distanceAA2(regions,i,binnum,dibins,dibins4): #Initiate empty array for storing histogram for directions, distances, and number of counted pairs in each distance range bin co0=nd.zeros(binnum-1,gpu(0),dtype="float32") codi0=nd.zeros((5,binnum-1),gpu(0),dtype="float32") count0=nd.zeros(binnum-1,gpu(0),dtype="float32") count4=nd.zeros((5,binnum-1),gpu(0),dtype="float32") co4=nd.zeros((5,binnum-1),gpu(0),dtype="float32") seed=nd.zeros((1,2),gpu(0)) #Calculate index coordinates and directions by chuncks a=regions[i[0]*broadcdp:min((i[0]+1)*broadcdp,regions.shape[0]),:] b=regions[i[1]*broadcdp:min((i[1]+1)*broadcdp,regions.shape[0]),:] a1=nd.array(a,gpu(0)) b1=nd.array(b,gpu(0)) # print ("a1",a1,"b1",b1) for ii in range (a1.shape[0]-1): a1_b1=(nd.expand_dims(a1[ii].reshape((1,2)),axis=1)-b1[ii+1:,:]).reshape((a1[ii+1:,:].shape[0],2)) seed=nd.concat(seed,a1_b1,dim=0) if seed.shape[0]>1: x1_x2=seed[1:,0] y1_y2=seed[1:,1] labels=nd.zeros(x1_x2.shape[0],gpu(0),dtype="float32") sdi0=(nd.degrees(nd.arctan((y1_y2)/(x1_x2)))+90).reshape((-1,)) ldis=nd.broadcast_hypot(x1_x2,y1_y2).reshape((-1,)) #Change 0 to 180 so it can apply sum of boolean mask without losing values sdi0=nd.where(condition=(sdi0==0),x=labels+180,y=sdi0) #Store sum of distances co0 and histogram of directions in each range bin for p in range (0,binnum-1): booleanmask=nd.equal((ldis>=bins[p]),(ldis<bins[p+1])) count0[p]+=nd.nansum(booleanmask) co0[p]+=nd.nansum(ldis*booleanmask) #Exclue values not in distance range bin sdi1=nd.where(condition=(booleanmask==0),x=labels-1,y=sdi0) for q in range (0,5): booleanmaskdi=nd.equal((sdi1>=dibins[q]),(sdi1<dibins[q+1])) codi0[q,p]+=nd.nansum(booleanmaskdi) for k in range (0,5): booleanmaskdi=nd.equal((sdi0>=dibins4[k]),(sdi0<dibins4[k+1])) ldis0=ldis*booleanmaskdi for l in range (0,binnum-1): booleanmask=nd.equal((ldis0>=bins[l]),(ldis0<bins[l+1])) count4[k,l]+=nd.nansum(booleanmask) co4[k,l]+=nd.nansum(ldis0*booleanmask) codi0[0,:]+=codi0[4,:] codi0=codi0[0:4,:] count4[0,:]+=count4[4,:] count4=count4[0:4,:] co4[0,:]+=co4[4,:] co4=co4[0:4,:] return(co0,codi0,count0,co4,count4)
def distanceAATOPO(regions,i,binnum,dibins,dibins4,x,y,ctx): #Initiate empty array for storing histogram for directions, distances, and number of counted pairs in each distance range bin co0=nd.zeros(binnum-1,ctx[0],dtype="float32") codi0=nd.zeros((5,binnum-1),ctx[0],dtype="float32") count0=nd.zeros(binnum-1,ctx[0],dtype="float32") count4=nd.zeros((5,binnum-1),ctx[0],dtype="float32") co4=nd.zeros((5,binnum-1),ctx[0],dtype="float32") #Calculate index coordinates and directions by chuncks a=regions[i*broadcdp:min((i+1)*broadcdp,regions.shape[0]),:] a1=nd.array(a,ctx[0]) b1=nd.array([x,y],ctx[0]) a1_b1=(nd.expand_dims(a1,axis=1)-b1).reshape((-1,2)) x1_x2=a1_b1[:,0] y1_y2=a1_b1[:,1] #Find the rows where all equal zeros boolmask=(x1_x2==0)*(y1_y2==0) labels=nd.zeros(boolmask.shape[0],ctx[0],dtype="float32") sdi0=(nd.degrees(nd.arctan((y1_y2)/(x1_x2)))+90).reshape((-1,)) ldis=nd.broadcast_hypot(x1_x2,y1_y2).reshape((-1,)) #Change the zeros into -1 sdi0=nd.where(condition=boolmask,x=labels-1,y=sdi0) ldis=nd.where(condition=boolmask,x=labels-1,y=ldis) #Change 0 to 180 so it can apply sum of boolean mask without losing values sdi0=nd.where(condition=(sdi0==0),x=labels+180,y=sdi0) #Store sum of distances co0 and histogram of directions in each range bin for p in range (0,binnum-1): booleanmask=nd.equal((ldis>=bins[p]),(ldis<bins[p+1])) count0[p]+=nd.sum(booleanmask) co0[p]+=nd.sum(ldis*booleanmask) #Exclue values not in distance range bin sdi1=nd.where(condition=(booleanmask==0),x=labels-1,y=sdi0) for q in range (0,5): booleanmaskdi=nd.equal((sdi1>=dibins[q]),(sdi1<dibins[q+1])) codi0[q,p]+=nd.nansum(booleanmaskdi) for k in range (0,5): booleanmaskdi=nd.equal((sdi0>=dibins4[k]),(sdi0<dibins4[k+1])) ldis0=ldis*booleanmaskdi for l in range (0,binnum-1): booleanmask=nd.equal((ldis0>=bins[l]),(ldis0<bins[l+1])) count4[k,l]+=nd.sum(booleanmask) co4[k,l]+=nd.sum(ldis0*booleanmask) codi0[0,:]+=codi0[4,:] codi0=codi0[0:4,:] count4[0,:]+=count4[4,:] count4=count4[0:4,:] co4[0,:]+=co4[4,:] co4=co4[0:4,:] return(co0.asnumpy(),codi0.asnumpy(),count0.asnumpy(),co4.asnumpy(),count4.asnumpy())
def compute_rot(v): """Return the rotationnal matrix M so that M.v = ||v||e1.""" if v[0] >= 0: M = nd.eye(len(v)) else: M = -nd.eye(len(v)) for i in range(1, len(v)): if v[i] == 0: continue rot_minus_theta = nd.eye(len(v)) temp = nd.dot(M, v) theta = nd.arctan(temp[i] / temp[0]) c = nd.cos(theta) s = nd.sin(theta) rot_minus_theta[0, 0] = c rot_minus_theta[i, i] = c rot_minus_theta[0, i] = s rot_minus_theta[i, 0] = -s M = nd.dot(rot_minus_theta, M) return M
def arctan(x): return nd.arctan(x)
def check_arctan(): x = create_input_for_trigonometric_ops([-np.Inf, -1, 0, 1, np.Inf]) y = nd.arctan(x) # expected ouput for indices=(0, 1, -3, -2, -1) after applying arctan() expected_output = [-np.pi / 2, -np.pi / 4, 0, np.pi / 4, np.pi / 2] assert_correctness_of_trigonometric_ops(y, expected_output)
def flowdr(dem_fill,NoData,rows,cols,ctx,switch): ingrid = np.indices((rows, cols)) ingrid[0] # row indices ingrid[1] # column indices ingridxmx=nd.array(ingrid[1],ctx[0]).reshape((1,1,rows, cols)) ingridymx=nd.array(ingrid[0],ctx[0]).reshape((1,1,rows, cols)) dem_fillmx=nd.array(dem_fill,ctx[0]) demmx=dem_fillmx.reshape((1,1,rows, cols)) res=1 l=[0,1,2,3,4,5,6,7,0] direct=[1,2,4,8,16,32,64,128] direct_d=[[1,3],[2,6],[4,12],[8,24],[16,48],[32,96],[64,192],[128,129]] weight=[None]*8 weight1=[None]*8 convx=[None]*8 convy=[None]*8 convz=[None]*8 runlen=[1,ma.pow(2,0.5),1,ma.pow(2,0.5),1,ma.pow(2,0.5),1,ma.pow(2,0.5)]*res n = [[[] for x in range(3)] for x in range(8)]#create list to store normal vectors for each facet s = [None]*8 d = [None]*8 weight[0] = nd.array([[0, 0, 0], [0, 1, -1], [0, 0, 0]], gpu(0)) weight[1] = nd.array([[0, 0, -1], [0, 1, 0], [0, 0, 0]], gpu(0)) weight[2] = nd.array([[0, -1, 0], [0, 1, 0], [0, 0, 0]], gpu(0)) weight[3] = nd.array([[-1, 0, 0], [0, 1, 0], [0, 0, 0]], gpu(0)) weight[4] = nd.array([[0, 0, 0], [-1, 1, 0], [0, 0, 0]], gpu(0)) weight[5] = nd.array([[0, 0, 0], [0, 1, 0], [-1, 0, 0]], gpu(0)) weight[6] = nd.array([[0, 0, 0], [0, 1, 0], [0, -1, 0]], gpu(0)) weight[7] = nd.array([[0, 0, 0], [0, 1, 0], [0, 0, -1]], gpu(0)) weight1[0] = nd.array([[0, 0, 0], [0, 1, -10], [0, 0, 0]], gpu(0)) weight1[1] = nd.array([[0, 0, -10], [0, 1, 0], [0, 0, 0]], gpu(0)) weight1[2] = nd.array([[0, -10, 0], [0, 1, 0], [0, 0, 0]], gpu(0)) weight1[3] = nd.array([[-10, 0, 0], [0, 1, 0], [0, 0, 0]], gpu(0)) weight1[4] = nd.array([[0, 0, 0], [-10, 1, 0], [0, 0, 0]], gpu(0)) weight1[5] = nd.array([[0, 0, 0], [0, 1, 0], [-10, 0, 0]], gpu(0)) weight1[6] = nd.array([[0, 0, 0], [0, 1, 0], [0, -10, 0]], gpu(0)) weight1[7] = nd.array([[0, 0, 0], [0, 1, 0], [0, 0, -10]], gpu(0)) d0=nd.zeros((rows, cols),ctx[0],dtype='float32') dd=nd.zeros((rows, cols),ctx[0],dtype='float32') d_flat=nd.zeros((rows, cols),ctx[0],dtype='float32') flat=nd.zeros((rows, cols),ctx[0],dtype='float32') dep=nd.zeros((rows, cols),ctx[0],dtype='float32') high=nd.zeros((rows, cols),ctx[0],dtype='float32') fd=nd.zeros((rows, cols),ctx[0],dtype='float32')-999 d_compact=nd.zeros((rows, cols),ctx[0],dtype='float32')-1 for i in range(0,8): w=weight[i].reshape((1, 1, 3, 3)) convz[i] = nd.Convolution(data=demmx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off') convz[i]=convz[i][0,0,:,:] if switch==1 or 3: convx[i] = nd.Convolution(data=ingridxmx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off') convy[i] = nd.Convolution(data=ingridymx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off') convx[i]=convx[i][0,0,:,:] convy[i]=convy[i][0,0,:,:] if switch==1 or 3: for p in range(0,8):#8 facets from N-NE clockwise l0=l[p] l1=l[p+1] d[l0]=d0-999#Nodata value dmax=d0-999 smax=d0-999 n[l0][0]= convz[l0]*convy[l1]-convz[l1]*convy[l0]#nx n[l0][1]= convz[l0]*convx[l1]-convz[l1]*convx[l0]#ny n[l0][2]= convy[l0]*convx[l1]-convy[l1]*convx[l0]#nz #make boolean mask to determine direction d and slope s d[l0]=nd.where(condition=((n[l0][0]==0)*(n[l0][1]>=0)),x=d0,y=d[l0]) d[l0]=nd.where(condition=((n[l0][0]==0)*(n[l0][1])<0),x=d0+ma.pi,y=d[l0]) d[l0]=nd.where(condition=(n[l0][0]>0),x=ma.pi/2-nd.arctan(n[l0][1]/n[l0][0]),y=d[l0]) d[l0]=nd.where(condition=(n[l0][0]<0),x=3*ma.pi/2-nd.arctan(n[l0][1]/n[l0][0]),y=d[l0]) d[l0]=nd.where(condition=((convz[l0]<=0)*(convz[l1]<=0)),x=dmax,y=d[l0]) s[l0]=-nd.tan(nd.arccos(n[l0][2]/(nd.sqrt(nd.square(n[l0][0])+nd.square(n[l0][1])+nd.square(n[l0][2])))))#slope of the triangular facet s[l0]=nd.where(condition=((convz[l0]<=0)*(convz[l1]<=0)),x=smax,y=s[l0]) #Modify the scenario when the steepest slope is outside the 45 range of each facet dmax=nd.where(condition=((convz[l0]/runlen[l0]>=convz[l1]/runlen[l0])*(convz[l0]>0)),x=d0+ma.pi*l0/4,y=dmax) dmax=nd.where(condition=((convz[l0]/runlen[l0]<convz[l1]/runlen[l0])*(convz[l1]>0)),x=d0+ma.pi*(l0+1)/4,y=dmax) smax=nd.where(condition=((convz[l0]>=convz[l1])*(convz[l0]>0)),x=convz[l0]/runlen[l0],y=smax) smax=nd.where(condition=((convz[l0]<convz[l1])*(convz[l1]>0)),x=convz[l1]/runlen[l1],y=smax) d[l0]=nd.where(condition=((d[l0]<ma.pi*l0/4)+(d[l0]>ma.pi*l1/4)),x=dmax,y=d[l0]) s[l0]=nd.where(condition=((d[l0]<ma.pi*l0/4)+(d[l0]>ma.pi*l1/4)),x=smax,y=s[l0]) if switch==1: #flat and depressions indicator grid flat=(convz[l0]==0)+flat dep=(convz[l0]<0)+dep high=(convz[l0]>0)+high for q in range(0,8):#check if the 45 degree range angles need to be maintaied, otherwise delete (set to NoData) l0=l[q] l1=l[q+1] l2=l[q-1] dmax=d0-999 if q==0: dmax=nd.where(condition=(d[0]==d[1]),x=d[0],y=dmax) dmax=nd.where(condition=(d[0]==d[7]),x=d[0],y=dmax) d[0]=nd.where(condition=((d[0]==ma.pi*l0/4)+(d[0]==ma.pi*l1/4)),x=dmax,y=d[0]) else: dmax=nd.where(condition=(d[l0]==d[l1]),x=d[l0],y=dmax) dmax=nd.where(condition=(d[l0]==d[l2]),x=d[l0],y=dmax) d[l0]=nd.where(condition=((d[l0]==ma.pi*l0/4)+(d[l0]==ma.pi*l1/4)),x=dmax,y=d[l0]) #Check if flat or surface depression area. then lable with -1 or -10 respectively if switch==1: fd=nd.where(condition=(flat==8),x=d0-2,y=fd)#flats fd=nd.where(condition=(dep>=1)*(high==0),x=d0-3,y=fd)#high edge high_zero=nd.where(condition=(high==0),x=d0+1,y=d0) for j in range (0,8): if switch==1 or switch==2: d_flat=nd.where(condition=(convz[j]==0),x=d0+direct[j],y=d0)+d_flat if switch==1: flat_near=nd.where(condition=(convz[j]==0),x=d0+5,y=d0) dd1=high_zero+flat_near w=weight1[j].reshape((1, 1, 3, 3)) dd1=dd1.reshape((1,1,rows, cols)) conv_near= nd.Convolution(data=dd1, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off') conv_near= conv_near[0,0,:,:] dd=nd.where(condition=(conv_near==-5)+(conv_near==-59)+(conv_near==-54)+(conv_near==-4),x=d0+1,y=d0)+dd if switch==1 or switch==3: d_compact=nd.where(condition=(d[j]==ma.pi*j/4),x=d0+direct_d[j][0],y=d_compact) d_compact=nd.where(condition=(d[j]>j*ma.pi/4)*(d[j]<(j+1)*ma.pi/4),x=d0+direct_d[j][1],y=d_compact) if switch==1 or switch==3: d_compact=nd.where(condition=(dem_fillmx==d0+NoData),x=d0-999,y=d_compact)#NoData if switch==1: fd=nd.where(condition=(dd>=1)*(high>=1),x=d0-1,y=fd)#low edge fd=nd.where(condition=(dep==8),x=d0-10,y=fd)#lowest points in depressions return (fd.asnumpy(),d_compact.asnumpy(),d_flat.asnumpy()) if switch==2: return (d_flat.asnumpy()) if switch==3: return (d_compact.asnumpy())