def output_debug(self,kp0,ownership,iterstring,weights=None): try: from mylib import viz except: return n_models = self.models.shape[0] #self.params['n_models'] n_coeffs = self.flow_bases_u.shape[0] sz_ownership = ownership.shape[0] if sz_ownership < n_models: z = np.zeros((n_models-sz_ownership,ownership.shape[1]))==1 ownership = np.vstack((ownership,z)).copy() flow_u_all = np.zeros((n_models,self.flow_bases_u.shape[1])) flow_v_all = np.zeros((n_models,self.flow_bases_u.shape[1])) for m in range(n_models): flow_u_all[m] = self.models[m,:n_coeffs].dot(self.flow_bases_u) flow_v_all[m] = self.models[m,n_coeffs:].dot(self.flow_bases_v) if weights is None: u_combined,v_combined = self.get_flow_nearest(kp0,ownership) else: u_combined,v_combined = self.get_flow_nearest(kp0,weights) I_combined = viz.viz_flow(u_combined,v_combined) I_individuals = [] for m in range(n_models): I_individuals.append(viz.viz_flow( flow_u_all[m].reshape((self.pc_h,self.pc_w)), flow_v_all[m].reshape((self.pc_h,self.pc_w)))) n_x = int(np.ceil((n_models+2)/3.0)) n_y = 3 colors = np.argmax(ownership,axis=0) if have_plt: plt.figure(figsize=(n_x*8,n_y*4)) plt.subplot(n_y,n_x,1) plt.scatter(kp0[:,0],kp0[:,1],linewidths=0,s=10,c=colors,vmin=-1,vmax=n_models,cmap='cubehelix') plt.xlim([0,self.pc_w]) plt.ylim([self.pc_h,0]) plt.title('Point ownerships') plt.subplot(n_y,n_x,2) plt.imshow(I_combined) plt.title('Combined flow') for m in range(n_models): plt.subplot(n_y,n_x,3+m) plt.imshow(I_individuals[m]) inds = ownership[m] plt.scatter(kp0[inds,0],kp0[inds,1],linewidths=0,s=10,c='black') plt.xlim([0,self.pc_w]) plt.ylim([self.pc_h,0]) plt.title('Flow model {0}'.format(m)) plt.savefig('./output_{0}.png'.format(iterstring),dpi=100,bbox_inches='tight') plt.close()
def output_debug(self, kp0, ownership, iterstring, weights=None): try: from mylib import viz except: return n_models = self.models.shape[0] #self.params['n_models'] n_coeffs = self.flow_bases_u.shape[0] sz_ownership = ownership.shape[0] if sz_ownership < n_models: z = np.zeros((n_models - sz_ownership, ownership.shape[1])) == 1 ownership = np.vstack((ownership, z)).copy() flow_u_all = np.zeros((n_models, self.flow_bases_u.shape[1])) flow_v_all = np.zeros((n_models, self.flow_bases_u.shape[1])) for m in range(n_models): flow_u_all[m] = self.models[m, :n_coeffs].dot(self.flow_bases_u) flow_v_all[m] = self.models[m, n_coeffs:].dot(self.flow_bases_v) if weights is None: u_combined, v_combined = self.get_flow_nearest(kp0, ownership) else: u_combined, v_combined = self.get_flow_nearest(kp0, weights) I_combined = viz.viz_flow(u_combined, v_combined) I_individuals = [] for m in range(n_models): I_individuals.append( viz.viz_flow(flow_u_all[m].reshape((self.pc_h, self.pc_w)), flow_v_all[m].reshape((self.pc_h, self.pc_w)))) n_x = int(np.ceil((n_models + 2) / 3.0)) n_y = 3 colors = np.argmax(ownership, axis=0) if have_plt: plt.figure(figsize=(n_x * 8, n_y * 4)) plt.subplot(n_y, n_x, 1) plt.scatter(kp0[:, 0], kp0[:, 1], linewidths=0, s=10, c=colors, vmin=-1, vmax=n_models, cmap='cubehelix') plt.xlim([0, self.pc_w]) plt.ylim([self.pc_h, 0]) plt.title('Point ownerships') plt.subplot(n_y, n_x, 2) plt.imshow(I_combined) plt.title('Combined flow') for m in range(n_models): plt.subplot(n_y, n_x, 3 + m) plt.imshow(I_individuals[m]) inds = ownership[m] plt.scatter(kp0[inds, 0], kp0[inds, 1], linewidths=0, s=10, c='black') plt.xlim([0, self.pc_w]) plt.ylim([self.pc_h, 0]) plt.title('Flow model {0}'.format(m)) plt.savefig('./output_{0}.png'.format(iterstring), dpi=100, bbox_inches='tight') plt.close()
def debug_GC(self, unaries_warp, unaries_colors, unaries_dist, tot, res, w_y, w_x, flow_u_all=None, flow_v_all=None): try: from mylib import viz except: return n_models = unaries_warp.shape[2] n_coeffs = self.flow_bases_u.shape[0] n_pixels = self.flow_bases_u.shape[1] I_warp = np.vstack([unaries_warp[:, :, m] for m in range(n_models)]) I_col = np.vstack([unaries_colors[:, :, m] for m in range(n_models)]) I_dist = np.vstack([unaries_dist[:, :, m] for m in range(n_models)]) I_tot = np.vstack([tot[:, :, m] for m in range(n_models)]) #I_tot = I_warp + I_col + I_dist if have_plt: plt.figure(figsize=(10, 10)) plt.subplot(1, 4, 1) plt.imshow(I_warp, cmap='hot') plt.title('Warp unaries') plt.subplot(1, 4, 2) plt.imshow(I_col, cmap='hot') plt.title('Color model unaries') plt.subplot(1, 4, 3) plt.imshow(I_dist, cmap='hot') plt.title('Distance unaries') plt.subplot(1, 4, 4) plt.imshow(I_tot, cmap='hot') plt.title('Combined unaries') plt.savefig('unaries.png', dpi=200, bbox_inches='tight') plt.close() if res is not None: plt.figure(figsize=(10, 10)) plt.imshow(res, cmap='hot') plt.title('Model results') plt.savefig('result_models.png', dpi=200, bbox_inches='tight') plt.close() plt.figure(figsize=(10, 10)) plt.subplot(2, 1, 1) plt.imshow(w_y, cmap='hot') plt.colorbar() plt.title('Vertical regularization weights') plt.subplot(2, 1, 2) plt.imshow(w_x, cmap='hot') plt.colorbar() plt.title('Horizontal reg weights') plt.savefig('reg_weights.png', dpi=200, bbox_inches='tight') plt.close() plt.figure(figsize=(10, 10)) plt.subplot(411) plt.imshow(np.argmin(unaries_warp, axis=2), cmap='hot') plt.title('min of warp unaries') plt.subplot(412) plt.imshow(np.argmin(unaries_colors, axis=2), cmap='hot') plt.title('min of color unaries') plt.subplot(413) plt.imshow(np.argmin(unaries_dist, axis=2), cmap='hot') plt.title('min of dist unaries') plt.subplot(414) plt.imshow(np.argmin(unaries_warp + unaries_colors + unaries_dist, axis=2), cmap='hot') plt.title('min of combined unaries') plt.savefig('unaries_min.png', dpi=200, bbox_inches='tight') plt.close() if flow_u_all is not None: plt.figure(figsize=(10, 10)) nx = 3 ny = int(np.ceil(float(n_models) / 3.0)) for n in range(n_models): u = flow_u_all[n].reshape((256, 512)) v = flow_v_all[n].reshape((256, 512)) Iv = viz.viz_flow(u, v) plt.subplot(ny, nx, n + 1) plt.imshow(Iv) plt.title('Model {}'.format(n)) plt.savefig('./models_all.png', dpi=200, bbox_inches='tight') plt.close()
def output_debug2(self, kp0, point_models, res, flow_u_all, flow_v_all): try: from mylib import misc except: return n_models = flow_u_all.shape[0] #self.params['n_models'] n_coeffs = self.flow_bases_u.shape[0] # sz_ownership = ownership.shape[0] # if sz_ownership < n_models: # z = np.zeros((n_models-sz_ownership,ownership.shape[1]))==1 # ownership = np.vstack((ownership,z)).copy() # I_individuals = [] for m in range(n_models): I_individuals.append( viz.viz_flow(flow_u_all[m].reshape((self.pc_h, self.pc_w)), flow_v_all[m].reshape((self.pc_h, self.pc_w)))) #colors = np.argmax(ownership,axis=0) colors = flow_u_all.shape[0] path = self.debug_path if have_plt: # Point ownershipts plt.figure(figsize=(10, 10)) plt.scatter(kp0[:, 0], kp0[:, 1], linewidths=0, s=40, c=point_models, vmin=0, vmax=n_models, cmap='cubehelix', alpha=0.5) #plt.axis('equal') plt.xlim([0, self.pc_w]) plt.ylim([self.pc_h, 0]) plt.xticks([], []) plt.yticks([], []) ax = plt.gca() ax.set_aspect('equal') #plt.title('Point ownerships') plt.savefig(os.path.join(path, 'ownerships.png'), dpi=200, bbox_inches='tight', pad_inches=0) plt.close() for m in range(n_models): plt.figure(figsize=(10, 10), frameon=False) plt.imshow(I_individuals[m]) #inds = ownership[m] inds = point_models == m plt.scatter(kp0[inds, 0], kp0[inds, 1], c='black', alpha=0.5, linewidths=(1, ), s=40) #print(kp0[inds,0]) #print(kp0[inds,1]) plt.xlim([0, self.pc_w]) plt.ylim([self.pc_h, 0]) plt.xticks([], []) plt.yticks([], []) #plt.title('Flow model {0}'.format(m)) if m == n_models - 1: outfname = 'model_homography.png' elif m == n_models - 2: outfname = 'model_pca.png' else: outfname = 'model_{0:02}.png'.format(m) plt.savefig(os.path.join(path, outfname), dpi=200, bbox_inches='tight', pad_inches=0) plt.close() plt.figure(figsize=(10, 10), frameon=False) plt.imshow(res, cmap='cubehelix', vmin=0, vmax=n_models - 1) #plt.title('Segmentation') #plt.colorbar() plt.xticks([], []) plt.yticks([], []) plt.savefig(os.path.join(path, 'segments.png'), dpi=200, bbox_inches='tight', pad_inches=0) plt.close()
def output_debug2(self,kp0,point_models,res,flow_u_all,flow_v_all): try: from mylib import misc except: return n_models = flow_u_all.shape[0] #self.params['n_models'] n_coeffs = self.flow_bases_u.shape[0] # sz_ownership = ownership.shape[0] # if sz_ownership < n_models: # z = np.zeros((n_models-sz_ownership,ownership.shape[1]))==1 # ownership = np.vstack((ownership,z)).copy() # I_individuals = [] for m in range(n_models): I_individuals.append(viz.viz_flow( flow_u_all[m].reshape((self.pc_h,self.pc_w)), flow_v_all[m].reshape((self.pc_h,self.pc_w)))) #colors = np.argmax(ownership,axis=0) colors = flow_u_all.shape[0] path = self.debug_path if have_plt: # Point ownershipts plt.figure(figsize=(10,10)) plt.scatter(kp0[:,0],kp0[:,1],linewidths=0,s=40,c=point_models,vmin=0,vmax=n_models,cmap='cubehelix',alpha=0.5) #plt.axis('equal') plt.xlim([0,self.pc_w]) plt.ylim([self.pc_h,0]) plt.xticks([],[]) plt.yticks([],[]) ax = plt.gca() ax.set_aspect('equal') #plt.title('Point ownerships') plt.savefig(os.path.join(path,'ownerships.png'),dpi=200,bbox_inches='tight',pad_inches=0) plt.close() for m in range(n_models): plt.figure(figsize=(10,10),frameon=False) plt.imshow(I_individuals[m]) #inds = ownership[m] inds = point_models==m plt.scatter(kp0[inds,0],kp0[inds,1],c='black',alpha=0.5,linewidths=(1,),s=40) #print(kp0[inds,0]) #print(kp0[inds,1]) plt.xlim([0,self.pc_w]) plt.ylim([self.pc_h,0]) plt.xticks([],[]) plt.yticks([],[]) #plt.title('Flow model {0}'.format(m)) if m == n_models-1: outfname = 'model_homography.png' elif m == n_models-2: outfname = 'model_pca.png' else: outfname = 'model_{0:02}.png'.format(m) plt.savefig(os.path.join(path,outfname),dpi=200,bbox_inches='tight',pad_inches=0) plt.close() plt.figure(figsize=(10,10),frameon=False) plt.imshow(res,cmap='cubehelix',vmin=0,vmax=n_models-1) #plt.title('Segmentation') #plt.colorbar() plt.xticks([],[]) plt.yticks([],[]) plt.savefig(os.path.join(path,'segments.png'),dpi=200,bbox_inches='tight',pad_inches=0) plt.close()
def debug_GC(self,unaries_warp,unaries_colors,unaries_dist,tot,res,w_y,w_x,flow_u_all=None,flow_v_all=None): try: from mylib import viz except: return n_models = unaries_warp.shape[2] n_coeffs = self.flow_bases_u.shape[0] n_pixels = self.flow_bases_u.shape[1] I_warp = np.vstack([unaries_warp[:,:,m] for m in range(n_models)]) I_col = np.vstack([unaries_colors[:,:,m] for m in range(n_models)]) I_dist = np.vstack([unaries_dist[:,:,m] for m in range(n_models)]) I_tot = np.vstack([tot[:,:,m] for m in range(n_models)]) #I_tot = I_warp + I_col + I_dist if have_plt: plt.figure(figsize=(10,10)) plt.subplot(1,4,1) plt.imshow(I_warp,cmap='hot') plt.title('Warp unaries') plt.subplot(1,4,2) plt.imshow(I_col,cmap='hot') plt.title('Color model unaries') plt.subplot(1,4,3) plt.imshow(I_dist,cmap='hot') plt.title('Distance unaries') plt.subplot(1,4,4) plt.imshow(I_tot,cmap='hot') plt.title('Combined unaries') plt.savefig('unaries.png',dpi=200,bbox_inches='tight') plt.close() if res is not None: plt.figure(figsize=(10,10)) plt.imshow(res,cmap='hot') plt.title('Model results') plt.savefig('result_models.png',dpi=200,bbox_inches='tight') plt.close() plt.figure(figsize=(10,10)) plt.subplot(2,1,1) plt.imshow(w_y,cmap='hot') plt.colorbar() plt.title('Vertical regularization weights') plt.subplot(2,1,2) plt.imshow(w_x,cmap='hot') plt.colorbar() plt.title('Horizontal reg weights') plt.savefig('reg_weights.png',dpi=200,bbox_inches='tight') plt.close() plt.figure(figsize=(10,10)) plt.subplot(411) plt.imshow(np.argmin(unaries_warp,axis=2),cmap='hot') plt.title('min of warp unaries') plt.subplot(412) plt.imshow(np.argmin(unaries_colors,axis=2),cmap='hot') plt.title('min of color unaries') plt.subplot(413) plt.imshow(np.argmin(unaries_dist,axis=2),cmap='hot') plt.title('min of dist unaries') plt.subplot(414) plt.imshow(np.argmin(unaries_warp + unaries_colors + unaries_dist,axis=2),cmap='hot') plt.title('min of combined unaries') plt.savefig('unaries_min.png',dpi=200,bbox_inches='tight') plt.close() if flow_u_all is not None: plt.figure(figsize=(10,10)) nx = 3 ny = int(np.ceil(float(n_models)/3.0)) for n in range(n_models): u = flow_u_all[n].reshape((256,512)) v = flow_v_all[n].reshape((256,512)) Iv = viz.viz_flow(u,v) plt.subplot(ny,nx,n+1) plt.imshow(Iv) plt.title('Model {}'.format(n)) plt.savefig('./models_all.png',dpi=200,bbox_inches='tight') plt.close()