def show_paf(img,paf,stride = 5,thres=0.1): """ @param img: ndarry, HxWx3 @param paf: ndarry, HxWxN """ paf = transform.rescale(paf,img.shape[0]/paf.shape[0]) h,w,n = paf.shape mask = (paf**2).reshape(h,w,n/2,2).sum(axis=3).sum(axis=2)<thres paf[mask] = 0 # img_size = img.shape[:2] X,Y = np.meshgrid(np.arange(0,w),np.arange(0,h)) plt.imshow(img, alpha=0.5) res = plt.quiver( X[::stride,::stride], Y[::stride,::stride], -paf[::stride,::stride,::2].sum(axis=2), paf[::stride,::stride,1::2].sum(axis=2), scale=20, units='width', headaxislength=0.01, alpha=.8, width=0.001, color='r') return res
plt.gca().invert_yaxis() plt.subplot(4,1,2) plt.plot(posT[:,0],posT[:,1],'.b',label='GPU') plt.gca().invert_yaxis() plt.legend() plt.subplot(4,1,3) plt.plot(my_dict['CPU_results'][0,:],my_dict['CPU_results'][1,:],'.r',label='CPU') plt.plot(posT[:,0],posT[:,1],'.b',label='GPU') plt.gca().invert_yaxis() plt.legend() plt.subplot(4,1,4) for i in range(0,N,32): plt.plot(posH[i::N,0],posH[i::N,1],'r') # plt.plot(posH[i::N,0],posH[i::N,1],'r') # plt.plot(posH[i,0],posH[i,1],'bo') plt.gca().invert_yaxis() plt.legend() plt.figure() # plt.plot(v[:,0],v[:,1],'.b',label='GPU velocities') plt.quiver(my_dict['pts_at_0'][:,0][::10], my_dict['pts_at_0'][:,1][::10], v[:,0][::10],v[:,1][::10]) plt.gca().invert_yaxis() plt.axis('scaled') plt.legend() pylab.show() #raw_input()
def quiver(self,x,v,scale,ds=16,color='k',negate_vy=False,pivot='middle', head=True,width=None): """ If width is None, its its value will be dictated by the value of head """ if head: headlength=5 headwidth=3 headaxislength=4.5 if width is None: width=.005 else: headlength=0 headwidth=0 headaxislength=0 if width is None: width=.003 if x is None: raise ValueError # if x is None: # x=self.xx # y=self.yy # if x.size != v[:,0].size: # x=self.x_img # y=self.y_img # # else: # if x.ndim != 2: # raise ValueError(x.shape) # if x.shape[1]!=2: # raise ValueError(x.shape) # # x,y=x[:,0].copy(),x[:,1].copy() # if x.size != v[:,0].size: # raise ValueError(x.shape,v.shape) if x.size != v.size: raise ValueError(x.shape,v.shape) if v.ndim != 2: raise ValueError(v.shape) if v.shape[1]!=2: raise ValueError(v.shape) if x.shape != v.shape: if x.ndim !=3 or x.shape[0]!=2: raise ValueError(x.shape) # x = np.asarray([x[0].flatten(),x[1].flatten()]).T v = np.asarray([v.cpu[:,0].reshape(x.shape[1],x.shape[2]), v.cpu[:,1].reshape(x.shape[1],x.shape[2])]) if x.shape != v.shape: raise ValueError(x.shape,v.shape) # if x.ndim != 2: # raise ValueError(x.shape) # if y.ndim != 2: # raise ValueError(x.shape) # try: # vx = v[:,0].reshape(x.shape) # vy = v[:,1].reshape(x.shape) # except: # raise ValueError(v.shape,x.shape) # if x.shape[1]!=2: # raise NotImplementedError(x.shape) # if v.shape[1]!=2: # raise NotImplementedError(x.shape) if x.ndim !=3 and x.shape[1]!=2: raise ValueError(x.shape) if v.ndim !=3 and v.shape[1]!=2: raise ValueError(v.shape) # _x,_y = x.T # vx,vy = v.T if x.ndim == 2: _x,_y = x.T _u,_v = v.T else: _x,_y = x _u,_v = v if negate_vy: _v = -_v # print scale,ds # 1/0 if _x.ndim==2: plt.quiver(_x[::ds,::ds],_y[::ds,::ds],_u[::ds,::ds],_v[::ds,::ds], angles='xy', scale_units='xy',scale=scale, pivot=pivot, color=color, headlength=headlength, headwidth=headwidth, headaxislength=headaxislength, width=width ) else: plt.quiver(_x[::ds],_y[::ds],_u[::ds],_v[::ds], angles='xy', scale_units='xy',scale=scale, pivot=pivot, color=color, headlength=headlength, headwidth=headwidth, headaxislength=headaxislength, width=width )
plt.subplot(4, 1, 2) plt.plot(posT[:, 0], posT[:, 1], '.b', label='GPU') plt.gca().invert_yaxis() plt.legend() plt.subplot(4, 1, 3) plt.plot(my_dict['CPU_results'][0, :], my_dict['CPU_results'][1, :], '.r', label='CPU') plt.plot(posT[:, 0], posT[:, 1], '.b', label='GPU') plt.gca().invert_yaxis() plt.legend() plt.subplot(4, 1, 4) for i in range(0, N, 32): plt.plot(posH[i::N, 0], posH[i::N, 1], 'r') # plt.plot(posH[i::N,0],posH[i::N,1],'r') # plt.plot(posH[i,0],posH[i,1],'bo') plt.gca().invert_yaxis() plt.legend() plt.figure() # plt.plot(v[:,0],v[:,1],'.b',label='GPU velocities') plt.quiver(my_dict['pts_at_0'][:, 0][::10], my_dict['pts_at_0'][:, 1][::10], v[:, 0][::10], v[:, 1][::10]) plt.gca().invert_yaxis() plt.axis('scaled') plt.legend() pylab.show() #raw_input()
def quiver(self, x, v, scale, ds=16, color='k', negate_vy=False, pivot='middle', head=True, width=None): """ If width is None, its its value will be dictated by the value of head """ if head: headlength = 5 headwidth = 3 headaxislength = 4.5 if width is None: width = .005 else: headlength = 0 headwidth = 0 headaxislength = 0 if width is None: width = .003 if x is None: raise ValueError # if x is None: # x=self.xx # y=self.yy # if x.size != v[:,0].size: # x=self.x_img # y=self.y_img # # else: # if x.ndim != 2: # raise ValueError(x.shape) # if x.shape[1]!=2: # raise ValueError(x.shape) # # x,y=x[:,0].copy(),x[:,1].copy() # if x.size != v[:,0].size: # raise ValueError(x.shape,v.shape) if x.size != v.size: raise ValueError(x.shape, v.shape) if v.ndim != 2: raise ValueError(v.shape) if v.shape[1] != 2: raise ValueError(v.shape) if x.shape != v.shape: if x.ndim != 3 or x.shape[0] != 2: raise ValueError(x.shape) # x = np.asarray([x[0].flatten(),x[1].flatten()]).T v = np.asarray([ v.cpu[:, 0].reshape(x.shape[1], x.shape[2]), v.cpu[:, 1].reshape(x.shape[1], x.shape[2]) ]) if x.shape != v.shape: raise ValueError(x.shape, v.shape) # if x.ndim != 2: # raise ValueError(x.shape) # if y.ndim != 2: # raise ValueError(x.shape) # try: # vx = v[:,0].reshape(x.shape) # vy = v[:,1].reshape(x.shape) # except: # raise ValueError(v.shape,x.shape) # if x.shape[1]!=2: # raise NotImplementedError(x.shape) # if v.shape[1]!=2: # raise NotImplementedError(x.shape) if x.ndim != 3 and x.shape[1] != 2: raise ValueError(x.shape) if v.ndim != 3 and v.shape[1] != 2: raise ValueError(v.shape) # _x,_y = x.T # vx,vy = v.T if x.ndim == 2: _x, _y = x.T _u, _v = v.T else: _x, _y = x _u, _v = v if negate_vy: _v = -_v # print scale,ds # 1/0 if _x.ndim == 2: plt.quiver(_x[::ds, ::ds], _y[::ds, ::ds], _u[::ds, ::ds], _v[::ds, ::ds], angles='xy', scale_units='xy', scale=scale, pivot=pivot, color=color, headlength=headlength, headwidth=headwidth, headaxislength=headaxislength, width=width) else: plt.quiver(_x[::ds], _y[::ds], _u[::ds], _v[::ds], angles='xy', scale_units='xy', scale=scale, pivot=pivot, color=color, headlength=headlength, headwidth=headwidth, headaxislength=headaxislength, width=width)