def draw_activation_layer(input_pts, input_qualities, log_dir, layer='layer1'): """input_pts: (num_point, 3) input_qualities: (num_point, C) """ total_path = ROOT_DIR + "/" + log_dir + "/" + layer + "/" if not os.path.exists(total_path): os.makedirs(total_path) feature_diml = input_qualities.shape[-1] for i in range(feature_diml): quality = input_qualities[:, i] quality1 = copy.deepcopy(quality) quality2 = copy.deepcopy(quality) quality3 = copy.deepcopy(quality) diameter = 5 img1 = pc_util.draw_point_cloud(input_pts, quality1, zrot=110 / 180.0 * np.pi, xrot=45 / 180.0 * np.pi, yrot=0 / 180.0 * np.pi, color=True, percentage=1, add_diameter=diameter) img2 = pc_util.draw_point_cloud(input_pts, quality2, zrot=70 / 180.0 * np.pi, xrot=135 / 180.0 * np.pi, yrot=90 / 180.0 * np.pi, color=True, percentage=1, add_diameter=diameter) img3 = pc_util.draw_point_cloud(input_pts, quality3, zrot=180.0 / 180.0 * np.pi, xrot=90 / 180.0 * np.pi, yrot=0 / 180.0 * np.pi, color=True, percentage=1, add_diameter=diameter) im_array = np.concatenate([img1, img2, img3], 1) img = Image.fromarray(np.uint8(im_array * 255.0), 'RGB') img_path = total_path + str(i) + ".jpg" img.save(img_path) ################################################################## html_name = "vis_" + layer + ".html" index_path = os.path.join(total_path, html_name) index = open(index_path, "w") index.write("<html><body><table><tr>") # get sample list items = os.listdir(total_path) items.sort() # write img to file for item in items: if item.endswith(".jpg"): index.write("<tr>") index.write("<td>%s</td>" % item) id = item.split(".")[0] img_path = "%s.jpg" % (id) index.write("<td><img width='100%%', src='%s'></td>" % img_path) index.close()
def draw_activation(input_pts, input_qualities, log_dir, folder, method='region'): """input_pts: (M, num_point, 3) input_qualities: (M, num_point, 1) state: eval or test epoch: the iteration of training method: region or point """ model_num = input_pts.shape[0] total_path = ROOT_DIR + "/" + log_dir + "/" + folder + "/html/" print(total_path) if not os.path.exists(total_path): os.makedirs(total_path) for i in range(model_num): input_gt = input_pts[i, :, :] quality = input_qualities[i, :, 0] quality1 = copy.deepcopy(quality) quality2 = copy.deepcopy(quality) quality3 = copy.deepcopy(quality) if method == 'point': diameter = 5 else: diameter = 5 img1 = pc_util.draw_point_cloud(input_gt, quality1, add_diameter=diameter, zrot=110 / 180.0 * np.pi, xrot=45 / 180.0 * np.pi, yrot=0 / 180.0 * np.pi, normalize=False, color=True) img2 = pc_util.draw_point_cloud(input_gt, quality2, add_diameter=diameter, zrot=70 / 180.0 * np.pi, xrot=135 / 180.0 * np.pi, yrot=90 / 180.0 * np.pi, normalize=False, color=True) img3 = pc_util.draw_point_cloud(input_gt, quality3, add_diameter=diameter, zrot=180.0 / 180.0 * np.pi, xrot=90 / 180.0 * np.pi, yrot=0 / 180.0 * np.pi, normalize=False, color=True) im_array = np.concatenate([img1, img2, img3], 1) img = Image.fromarray(np.uint8(im_array * 255.0), 'RGB') img_path = total_path + str(i) + ".jpg" img.save(img_path) ################################################################## html_name = "vis_" + method + ".html" index_path = os.path.join(total_path, html_name) index = open(index_path, "w") index.write("<html><body><table><tr>") # get sample list items = os.listdir(total_path) items.sort() # write img to file for item in items: if item.endswith(".jpg"): index.write("<tr>") index.write("<td>%s</td>" % item) id = item.split(".")[0] img_path = "%s.jpg" % (id) index.write("<td><img width='100%%', src='%s'></td>" % img_path) index.close()
def eval_one_epoch(sess, ops, num_votes=1, topk=1): error_cnt = 0 is_training = False total_correct = 0 total_seen = 0 loss_sum = 0 total_seen_class = [0 for _ in range(NUM_CLASSES)] total_correct_class = [0 for _ in range(NUM_CLASSES)] fout = open(os.path.join(DUMP_DIR, 'pred_label.txt'), 'w') for fn in range(len(TEST_FILES)): log_string('----'+str(fn)+'----') current_data, current_label = provider.loadDataFile(TEST_FILES[fn]) current_data = current_data[:,0:NUM_POINT,:] current_label = np.squeeze(current_label) file_size = current_data.shape[0] print(file_size) # set by wind: # my code is based on batch_size = 1 # set batch_size = 1 for this file for batch_idx in range(file_size): # for batch_idx in np.array([1,710,1805,385,703,1501])-1: print(batch_idx) start_idx = batch_idx end_idx = batch_idx + 1 cur_batch_size = 1 no_influence_position = current_data[start_idx,0,:].copy() global_feature_list = [] orgin_data = current_data[start_idx,:,:].copy() #------------------------------------------------------------------- # save origin data #------------------------------------------------------------------- fileName = 'dataAnalysis/figures/%d_orgin_points' % (start_idx) img_filename = fileName + '.jpg' # plyFileName = fileName + '.ply' # pc_util.write_ply(np.squeeze(orgin_data),plyFileName) # pc_util.pyplot_draw_point_cloud(np.squeeze( orgin_data ),'') output_img = pc_util.draw_point_cloud(np.squeeze(orgin_data)) scipy.misc.imsave(img_filename, output_img) #------------------------------------------------------------------- # get global matrix #------------------------------------------------------------------- feed_dict = {ops['pointclouds_pl']: current_data[start_idx:end_idx, :, :] , ops['labels_pl']: current_label[start_idx:end_idx], ops['is_training_pl']: is_training} global_matrix = np.array(sess.run(ops['global_matrix'], feed_dict=feed_dict)) global_matrix = global_matrix[global_matrix[:,0].argsort()] # print(global_matrix.shape) # plt.imshow(global_matrix, interpolation='nearest') # plt.show() fileName = 'dataAnalysis/figures/%d/%d_global_matrix' % (current_label[start_idx], batch_idx) img_filename = fileName + '.png' if not os.path.exists(os.path.dirname(img_filename)): try: os.makedirs(os.path.dirname(img_filename)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise scipy.misc.imsave(img_filename, global_matrix) # #------------------------------------------------------------------- # # get critical points # #------------------------------------------------------------------- # for change_point in range(NUM_POINT): # current_data[start_idx, change_point, :] = no_influence_position.copy() # # for change_point in range(NUM_POINT): # current_data[start_idx, change_point, :] = orgin_data[change_point, :].copy() # # Aggregating BEG # for vote_idx in range(num_votes): # if FLAGS.model == 'pointnet_cls_basic': # rotated_data = current_data[start_idx:end_idx, :, :] # directional pointNet # else: # rotated_data = provider.rotate_point_cloud_by_angle(current_data[start_idx:end_idx, :, :], # vote_idx/float(num_votes) * np.pi * 2) # pointNet # feed_dict = {ops['pointclouds_pl']: rotated_data, # ops['labels_pl']: current_label[start_idx:end_idx], # ops['is_training_pl']: is_training} # # global_feature_val = sess.run(ops['global_feature'], # feed_dict=feed_dict) # # global_feature_list.append(global_feature_val) # # critical_points = [] # max_feature = np.zeros(global_feature_list[0].size) - 10 # feature_points = np.zeros(global_feature_list[0].size) # for index in range(len(global_feature_list)): # #distance = math.sqrt(((global_feature_list[index] - global_feature_list[-1]) ** 2).sum()) # #distance_list.append(distance) # top = global_feature_list[index] # feature_points = np.where(top > max_feature, index, feature_points) # max_feature = np.where(top > max_feature, top, max_feature) # # for index in feature_points[0]: # critical_points.append(orgin_data[int(index), :]) # critical_points = list(set([tuple(t) for t in critical_points])) # # fileName = 'dataAnalysis/figures/%d_critical_points' % (start_idx) # img_filename = fileName + '.jpg' # plyFileName = fileName + '.ply' # pc_util.write_ply(np.squeeze( critical_points),plyFileName) ## pc_util.pyplot_draw_point_cloud(np.squeeze( critical_points ),'') # output_img = pc_util.draw_point_cloud(np.squeeze( critical_points)) # scipy.misc.imsave(img_filename, output_img) # #------------------------------------------------------------------- # # get upper-bound points # #------------------------------------------------------------------- # upper_bound_points = np.empty_like(orgin_data.shape) # upper_bound_points = orgin_data.copy() # current_data[start_idx,:,:] = orgin_data.copy() # # search_step = 0.05 # stand_feature = np.empty_like(global_feature_list[-1].shape) # max_position = [-0.5,-0.5,-0.5] # min_position = [0.5, 0.5, 0.5] # # for point_index in range(NUM_POINT): # max_position = np.maximum(max_position, current_data[start_idx,point_index,:]) # min_position = np.minimum(min_position, current_data[start_idx,point_index,:]) # # print(max_position) # print(min_position) # for vote_idx in range(num_votes): # if FLAGS.model == 'pointnet_cls_basic': # rotated_data = current_data[start_idx:end_idx, :, :] # directional pointNet # else: # rotated_data = provider.rotate_point_cloud_by_angle(current_data[start_idx:end_idx, :, :], # vote_idx/float(num_votes) * np.pi * 2) # pointNet # # feed_dict = {ops['pointclouds_pl']: rotated_data, # ops['labels_pl']: current_label[start_idx:end_idx], # ops['is_training_pl']: is_training} # # # global_feature_val = sess.run(ops['global_feature'],feed_dict=feed_dict) # stand_feature = global_feature_val.copy() # # change_point = 0 # current_data[start_idx,:,:] = orgin_data.copy() # for point_index in range(NUM_POINT): # if not (point_index in feature_points[0]): # change_point = point_index # break # # for x in np.linspace(min_position[0], max_position[0], (max_position[0]-min_position[0])//search_step +1): # for y in np.linspace(min_position[1], max_position[1], (max_position[1]-min_position[1])//search_step +1): # for z in np.linspace(min_position[2], max_position[2], (max_position[2]-min_position[2])//search_step +1): # current_data[start_idx,change_point,:] = (x,y,z) #+ orgin_position # # # Aggregating BEG # for vote_idx in range(num_votes): # # if FLAGS.model == 'pointnet_cls_basic': # rotated_data = current_data[start_idx:end_idx, :, :] # directional pointNet # else: # rotated_data = provider.rotate_point_cloud_by_angle(current_data[start_idx:end_idx, :, :], # vote_idx/float(num_votes) * np.pi * 2) # pointNet # # feed_dict = {ops['pointclouds_pl']: rotated_data, # ops['labels_pl']: current_label[start_idx:end_idx], # ops['is_training_pl']: is_training} # # global_feature_val = sess.run(ops['global_feature'],feed_dict=feed_dict) # # if (global_feature_val <= stand_feature).all(): # upper_bound_points = np.append(upper_bound_points, np.array([[x,y,z]]),axis = 0) # # fileName = 'dataAnalysis/figures/%d_upper_bound_points' % (start_idx) # img_filename = fileName + '.jpg' # plyFileName = fileName + '.ply' # pc_util.write_ply(np.squeeze(upper_bound_points),plyFileName) ## pc_util.pyplot_draw_point_cloud(np.squeeze( upper_bound_points ),'') # output_img = pc_util.draw_point_cloud(np.squeeze(upper_bound_points)) # scipy.misc.imsave(img_filename, output_img) current_data[start_idx,:,:] = orgin_data.copy() print('------Finished!---------\n')