def eval_one_epoch(sess, ops, num_votes=12, topk=1): is_training = False current_data = np.empty([len(TRAIN_FILES), NUM_POINT, 3], dtype=float) labels = np.empty([len(TRAIN_FILES)], dtype=int) current_label = np.empty([len(TRAIN_FILES)], dtype=int) for fn in range(len(TRAIN_FILES)): cut1, cut2, label = provider.loadDataFile_cut_2(TRAIN_FILES[fn], False) # data, label = provider.loadDataFile(TRAIN_FILES[fn]) data = np.concatenate((cut1, cut2), axis=0) # data = cut1 idx = np.random.randint(data.shape[0], size=NUM_POINT) data = data[idx, :] label = np.squeeze(label) current_data[fn] = data current_label[fn] = 0 labels[fn] = label current_label = np.squeeze(current_label) file_size = current_data.shape[0] num_batches = file_size // BATCH_SIZE log_string('file_size: %d' % (file_size)) log_string('num_batches: %d' % (num_batches)) #save labels for test label_f = open('features/train_label.txt', 'w+') labels = labels[0:num_batches * BATCH_SIZE] np.savetxt(label_f, labels, fmt='%d') for vote_idx in range(num_votes): log_string('vote: %d' % (vote_idx)) feature_f = open('features/train_feature_' + str(vote_idx) + '.txt', 'w') for batch_idx in range(num_batches): start_idx = batch_idx * BATCH_SIZE end_idx = (batch_idx + 1) * BATCH_SIZE cur_batch_size = end_idx - start_idx print(batch_idx) rotated_data = provider.rotate_point_cloud_by_angle( current_data[start_idx:end_idx, :, :], vote_idx / float(num_votes) * np.pi * 2) feed_dict = { ops['pointclouds_pl']: rotated_data, ops['labels_pl']: current_label[start_idx:end_idx], ops['is_training_pl']: is_training } _, _, feat_out = sess.run( [ops['loss'], ops['pred'], ops['feature']], feed_dict=feed_dict) np.savetxt(feature_f, feat_out, fmt='%f')
import sys import os BASE_DIR = os.path.dirname(os.path.abspath(__file__)) sys.path.append(BASE_DIR) sys.path.append(os.path.join(BASE_DIR, 'models')) sys.path.append(os.path.join(BASE_DIR, 'utils')) import provider TRAIN_FILES = provider.getDataFiles(\ os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048_cut/train_files.txt')) gt_labels = np.empty([len(TRAIN_FILES),1], dtype=int) print('Loading groundtruth labels ...') for fn in range(len(TRAIN_FILES)): _, _, label = provider.loadDataFile_cut_2(TRAIN_FILES[fn], False) label = np.squeeze(label) gt_labels[fn] = label CLASS_NUM = 16 classes = np.zeros((CLASS_NUM, CLASS_NUM)) print('Loading cluster labels ...') cluster_f = open("cluster_label.txt", "r") cluster_labels = cluster_f.readlines() cluster_labels = [int(i) for i in cluster_labels] print('Caculating proportion ...') for i in range(len(cluster_labels)): gt_l = gt_labels[i] ct_l = cluster_labels[i]
def train_one_epoch(sess, ops, train_writer): """ ops: dict mapping from string to tf ops """ is_training = True # Shuffle train files train_file_idxs = np.arange(0, len(TRAIN_FILES)) np.random.shuffle(train_file_idxs) current_data_1 = np.empty([3 * len(TRAIN_FILES), NUM_POINT, 3], dtype=float) current_data_2 = np.empty([3 * len(TRAIN_FILES), NUM_POINT, 3], dtype=float) current_label = np.empty([3 * len(TRAIN_FILES), 1], dtype=int) fn = 0 count = 0 while fn < len(TRAIN_FILES) - 1: # log_string('----' + str(fn) + '-----') total_current = [] a1, a2, _ = provider.loadDataFile_cut_2( TRAIN_FILES[train_file_idxs[fn]]) idx = np.random.randint(a1.shape[0], size=NUM_POINT) a1 = a1[idx, :] idx = np.random.randint(a2.shape[0], size=NUM_POINT) a2 = a2[idx, :] total_current.append(a1) total_current.append(a2) fn = fn + 1 b1, b2, _ = provider.loadDataFile_cut_2( TRAIN_FILES[train_file_idxs[fn]]) idx = np.random.randint(b1.shape[0], size=NUM_POINT) b1 = b1[idx, :] idx = np.random.randint(b2.shape[0], size=NUM_POINT) b2 = b2[idx, :] total_current.append(b1) total_current.append(b2) fn = fn + 1 pair_num = 0 for index in range(len(total_current)): for index2 in range(index + 1, len(total_current)): current_data_1[6 * count + pair_num, :, :] = total_current[index] current_data_2[6 * count + pair_num, :, :] = total_current[index2] if (index < 2) and (index2 >= 2): current_label[6 * count + pair_num, :] = 0 else: current_label[6 * count + pair_num, :] = 1 pair_num = pair_num + 1 count = count + 1 current_label = np.squeeze(current_label) file_size = current_data_1.shape[0] num_batches = file_size // BATCH_SIZE total_correct = 0 total_seen = 0 loss_sum = 0 for batch_idx in range(num_batches): start_idx = batch_idx * BATCH_SIZE end_idx = (batch_idx + 1) * BATCH_SIZE # shuffle each batch data_1 = current_data_1[start_idx:end_idx, :, :] data_2 = current_data_2[start_idx:end_idx, :, :] label = current_label[start_idx:end_idx] combine_data = np.concatenate((data_1, data_2), axis=2) combine_data, label, _ = provider.shuffle_data(combine_data, np.squeeze(label)) data_1 = combine_data[:, :, 0:3] data_2 = combine_data[:, :, 3:6] label = np.squeeze(label) # Augment batched point clouds by rotation and jittering rotated_data_1 = provider.rotate_point_cloud(data_1) jittered_data_1 = provider.jitter_point_cloud(rotated_data_1) jittered_data_1 = provider.random_scale_point_cloud(jittered_data_1) jittered_data_1 = provider.rotate_perturbation_point_cloud( jittered_data_1) jittered_data_1 = provider.shift_point_cloud(jittered_data_1) rotated_data_2 = provider.rotate_point_cloud(data_2) jittered_data_2 = provider.jitter_point_cloud(rotated_data_2) jittered_data_2 = provider.random_scale_point_cloud(jittered_data_2) jittered_data_2 = provider.rotate_perturbation_point_cloud( jittered_data_2) jittered_data_2 = provider.shift_point_cloud(jittered_data_2) feed_dict = { ops['pointclouds_pl_1']: jittered_data_1, ops['pointclouds_pl_2']: jittered_data_2, ops['labels_pl']: label, ops['is_training_pl']: is_training } summary, step, _, loss_val, pred_val = sess.run([ ops['merged'], ops['step'], ops['train_op'], ops['loss'], ops['pred'] ], feed_dict=feed_dict) train_writer.add_summary(summary, step) pred_val = np.argmax(pred_val, 1) correct = np.sum(pred_val == label) total_correct += correct total_seen += BATCH_SIZE loss_sum += loss_val if batch_idx % 50 == 0: log_string('mean loss: {0:f} accuracy: {1:f}'.format( loss_sum / float(batch_idx + 1), total_correct / float(total_seen))) log_string('mean loss: {0:f} accuracy: {1:f}'.format( loss_sum / float(batch_idx + 1), total_correct / float(total_seen)))
def train_one_epoch(sess, ops, train_writer): """ ops: dict mapping from string to tf ops """ is_training = True # Shuffle train files train_file_idxs = np.arange(0, len(LABELS)) np.random.shuffle(train_file_idxs) current_data = np.empty([len(LABELS), NUM_POINT, 3], dtype=float) current_label = np.empty([len(LABELS), 1], dtype=int) for fn in range(len(LABELS)): cut1, cut2, _ = provider.loadDataFile_cut_2( TRAIN_FILES[train_file_idxs[fn]], False) idx = np.random.randint(cut1.shape[0], size=NUM_POINT / 2) cut1 = cut1[idx, :] idx = np.random.randint(cut2.shape[0], size=NUM_POINT / 2) cut2 = cut2[idx, :] current_data[fn] = np.concatenate((cut1, cut2), axis=0) current_label[fn] = LABELS[train_file_idxs[fn]] current_label = np.squeeze(current_label) file_size = current_data.shape[0] num_batches = file_size // BATCH_SIZE total_correct = 0 total_seen = 0 loss_sum = 0 for batch_idx in range(num_batches): start_idx = batch_idx * BATCH_SIZE end_idx = (batch_idx + 1) * BATCH_SIZE # Augment batched point clouds by rotation and jittering rotated_data = provider.rotate_point_cloud( current_data[start_idx:end_idx, :, :]) jittered_data = provider.jitter_point_cloud(rotated_data) jittered_data = provider.random_scale_point_cloud(jittered_data) jittered_data = provider.rotate_perturbation_point_cloud(jittered_data) jittered_data = provider.shift_point_cloud(jittered_data) feed_dict = { ops['pointclouds_pl']: jittered_data, ops['labels_pl']: current_label[start_idx:end_idx], ops['is_training_pl']: is_training, } summary, step, _, loss_val, pred_val, feature = sess.run( [ ops['merged'], ops['step'], ops['train_op'], ops['loss'], ops['pred'], ops['feat'] ], feed_dict=feed_dict) train_writer.add_summary(summary, step) pred_val = np.argmax(pred_val, 1) correct = np.sum(pred_val == current_label[start_idx:end_idx]) total_correct += correct total_seen += BATCH_SIZE loss_sum += loss_val if batch_idx % 100 == 0: log_string('mean loss: {0:f} accuracy: {1:f}'.format( loss_sum / float(batch_idx + 1), total_correct / float(total_seen))) log_string('mean loss: {0:f} accuracy: {1:f}'.format( loss_sum / float(batch_idx + 1), total_correct / float(total_seen)))
def eval_one_epoch(sess, ops, num_votes=12, topk=1): is_training = False total_correct = 0 total_seen = 0 loss_sum = 0 current_data_1 = np.empty([3 * len(TEST_FILES), NUM_POINT, 3], dtype=float) current_data_2 = np.empty([3 * len(TEST_FILES), NUM_POINT, 3], dtype=float) current_label = np.empty([3 * len(TEST_FILES), 1], dtype=int) fn = 0 count = 0 while fn < len(TEST_FILES) - 1: total_current = [] a1, a2, _ = provider.loadDataFile_cut_2(TEST_FILES[fn]) idx = np.random.randint(a1.shape[0], size=NUM_POINT) a1 = a1[idx, :] idx = np.random.randint(a2.shape[0], size=NUM_POINT) a2 = a2[idx, :] total_current.append(a1) total_current.append(a2) fn = fn + 1 b1, b2, _ = provider.loadDataFile_cut_2(TEST_FILES[fn]) idx = np.random.randint(b1.shape[0], size=NUM_POINT) b1 = b1[idx, :] idx = np.random.randint(b2.shape[0], size=NUM_POINT) b2 = b2[idx, :] total_current.append(b1) total_current.append(b2) fn = fn + 1 pair_num = 0 for index in range(len(total_current)): for index2 in range(index + 1, len(total_current)): current_data_1[6 * count + pair_num, :, :] = total_current[index] current_data_2[6 * count + pair_num, :, :] = total_current[index2] if (index < 2) and (index2 >= 2): current_label[6 * count + pair_num, :] = 0 else: current_label[6 * count + pair_num, :] = 1 pair_num = pair_num + 1 count = count + 1 current_label = np.squeeze(current_label) file_size = current_data_1.shape[0] num_batches = file_size // BATCH_SIZE log_string('file_size: %d' % (file_size)) log_string('num_batches: %d' % (num_batches)) for batch_idx in range(num_batches): start_idx = batch_idx * BATCH_SIZE end_idx = (batch_idx + 1) * BATCH_SIZE cur_batch_size = end_idx - start_idx log_string('batch: %d' % (batch_idx)) # Aggregating BEG batch_loss_sum = 0 # sum of losses for the batch batch_pred_sum = np.zeros( (cur_batch_size, NUM_CLASSES)) # score for classes # batch_pred_classes = np.zeros((cur_batch_size, NUM_CLASSES)) # 0/1 for classes for vote_idx in range(num_votes): rotated_data_1 = provider.rotate_point_cloud_by_angle( current_data_1[start_idx:end_idx, :, :], vote_idx / float(num_votes) * np.pi * 2) rotated_data_2 = provider.rotate_point_cloud_by_angle( current_data_2[start_idx:end_idx, :, :], vote_idx / float(num_votes) * np.pi * 2) feed_dict = { ops['pointclouds_pl_1']: rotated_data_1, ops['pointclouds_pl_2']: rotated_data_2, ops['labels_pl']: current_label[start_idx:end_idx], ops['is_training_pl']: is_training } loss_val, pred_val, _ = sess.run( [ops['loss'], ops['pred'], ops['feature']], feed_dict=feed_dict) batch_pred_sum += pred_val # batch_pred_val = np.argmax(pred_val, 1) # for el_idx in range(cur_batch_size): # batch_pred_classes[el_idx, batch_pred_val[el_idx]] += 1 batch_loss_sum += (loss_val * cur_batch_size / float(num_votes)) # pred_val_topk = np.argsort(batch_pred_sum, axis=-1)[:,-1*np.array(range(topk))-1] # pred_val = np.argmax(batch_pred_classes, 1) pred_val = np.argmax(batch_pred_sum, 1) # Aggregating END correct = np.sum(pred_val == current_label[start_idx:end_idx]) # correct = np.sum(pred_val_topk[:,0:topk] == label_val) total_correct += correct total_seen += cur_batch_size loss_sum += batch_loss_sum log_string('eval mean loss: %f' % (loss_sum / float(total_seen))) log_string('eval accuracy: %f' % (total_correct / float(total_seen)))