def eval_one_epoch(stack, model, criterion, device, val_writer): num_batches = VALIDATION_DATASET.get_num_batches(BATCH_SIZE_VAL) # Reset metrics loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(num_classes) log_string(str(datetime.now())) log_string("---- EPOCH %03d EVALUATION ----" % (EPOCH_CNT)) update_progress(0) for batch_idx in range(num_batches): progress = float(batch_idx) / float(num_batches) update_progress(round(progress, 2)) batch_data, batch_label, batch_weights = stack.get() # Get predicted labels points_tensor = torch.from_numpy(batch_data).to( device, dtype=torch.float32) # (B, sample_num, 3) batch_label_tensor = torch.from_numpy(batch_label).to( device, dtype=torch.long) # (B, sample_num) model = model.eval() with torch.no_grad(): points_prob = run_model( model, points_tensor, PARAMS, MODEL_NAME) # (B, sample_num, num_classes), (B, sample_num, 3) batch_loss = criterion(points_prob, batch_label_tensor) _, points_pred = torch.max(points_prob, dim=2) # (B, sample_num) # Update metrics pred_val = points_pred.cpu().numpy() for i in range(len(pred_val)): for j in range(len(pred_val[i])): confusion_matrix.increment(batch_label[i][j], pred_val[i][j]) loss_sum += batch_loss.cpu().numpy() update_progress(1) iou_per_class = confusion_matrix.get_per_class_ious() # Display metrics log_string("mean loss: %f" % (loss_sum / float(num_batches))) log_string("Overall accuracy : %f" % (confusion_matrix.get_accuracy())) log_string("Average IoU : %f" % (confusion_matrix.get_mean_iou())) val_writer.add_scalar("%s mean loss" % DATASET_NAME, loss_sum / float(num_batches), EPOCH_CNT) val_writer.add_scalar("%s overall accuracy" % DATASET_NAME, confusion_matrix.get_accuracy(), EPOCH_CNT) val_writer.add_scalar("%s average IoU" % DATASET_NAME, confusion_matrix.get_mean_iou(), EPOCH_CNT) iou_per_class = [0] + iou_per_class # label 0 is ignored for i in range(1, num_classes): log_string("IoU of %s : %f" % (VALIDATION_DATASET.labels_names[i], iou_per_class[i])) return confusion_matrix.get_mean_iou()
def train_one_epoch(sess, ops, train_writer, stack): """Train one epoch Args: sess (tf.Session): the session to evaluate Tensors and ops ops (dict of tf.Operation): contain multiple operation mapped with with strings train_writer (tf.FileSaver): enable to log the training with TensorBoard compute_class_iou (bool): it takes time to compute the iou per class, so you can disable it here """ is_training = True num_batches = TRAIN_DATASET.get_num_batches(BATCH_SIZE) log_string(str(datetime.now())) update_progress(0) # Reset metrics loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(NUM_CLASSES) # Train over num_batches batches for batch_idx in range(num_batches): # Refill more batches if empty progress = float(batch_idx) / float(num_batches) update_progress(round(progress, 2)) batch_data, batch_label, batch_weights = stack.get() # Get predicted labels feed_dict = { ops['pointclouds_pl']: batch_data, ops['labels_pl']: batch_label, ops['smpws_pl']: batch_weights, 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'], ops['update_iou'] ], feed_dict=feed_dict) train_writer.add_summary(summary, step) pred_val = np.argmax(pred_val, 2) # Update metrics for i in range(len(pred_val)): for j in range(len(pred_val[i])): confusion_matrix.count_predicted(batch_label[i][j], pred_val[i][j]) loss_sum += loss_val update_progress(1) log_string('mean loss: %f' % (loss_sum / float(num_batches))) log_string("Overall accuracy : %f" % (confusion_matrix.get_overall_accuracy())) log_string("Average IoU : %f" % (confusion_matrix.get_average_intersection_union())) iou_per_class = confusion_matrix.get_intersection_union_per_class() for i in range(1, NUM_CLASSES): log_string("IoU of %s : %f" % (TRAIN_DATASET.labels_names[i], iou_per_class[i]))
def train_one_epoch(stack, scheduler, model, criterion, device, train_writer): global EPOCH_CNT num_batches = TRAIN_DATASET.get_num_batches(BATCH_SIZE_TRAIN) log_string(str(datetime.now())) update_progress(0) # Reset metrics loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(num_classes) # Train over num_batches batches for batch_idx in range(num_batches): # Refill more batches if empty progress = float(batch_idx) / float(num_batches) update_progress(round(progress, 2)) batch_data, batch_label, batch_weights = stack.get() # Get predicted labels points_tensor = torch.from_numpy(batch_data).to( device, dtype=torch.float32) # (B, sample_num, 3) batch_label_tensor = torch.from_numpy(batch_label).to( device, dtype=torch.long) # (B, sample_num) scheduler.optimizer.zero_grad() model = model.train() points_prob = run_model( model, points_tensor, PARAMS, MODEL_NAME) # (B, sample_num, num_classes), (B, sample_num, 3) batch_loss = criterion(points_prob, batch_label_tensor) _, points_pred = torch.max(points_prob, dim=2) # (B, sample_num) batch_loss.backward() scheduler.optimizer.step() scheduler.step() # Update metrics pred_val = points_pred.cpu().numpy() for i in range(len(pred_val)): for j in range(len(pred_val[i])): confusion_matrix.increment(batch_label[i][j], pred_val[i][j]) loss_sum += batch_loss.cpu().detach().numpy() update_progress(1) EPOCH_CNT += 1 log_string("mean loss: %f" % (loss_sum / float(num_batches))) log_string("Overall accuracy : %f" % (confusion_matrix.get_accuracy())) log_string("Average IoU : %f" % (confusion_matrix.get_mean_iou())) train_writer.add_scalar("%s mean loss" % DATASET_NAME, loss_sum / float(num_batches), EPOCH_CNT) train_writer.add_scalar("%s overall accuracy" % DATASET_NAME, confusion_matrix.get_accuracy(), EPOCH_CNT) train_writer.add_scalar("%s average IoU" % DATASET_NAME, confusion_matrix.get_mean_iou(), EPOCH_CNT) iou_per_class = confusion_matrix.get_per_class_ious() iou_per_class = [0] + iou_per_class # label 0 is ignored for i in range(1, num_classes): log_string("IoU of %s : %f" % (TRAIN_DATASET.labels_names[i], iou_per_class[i]))
def eval_one_epoch(sess, ops, test_writer): """Evaluate one epoch Args: sess (tf.Session): the session to evaluate tensors and operations ops (tf.Operation): the dict of operations test_writer (tf.summary.FileWriter): enable to log the evaluation on TensorBoard Returns: float: the overall accuracy computed on the test set """ global EPOCH_CNT is_training = False test_idxs = np.arange(0, len(TEST_DATASET)) num_batches = len(TEST_DATASET) / BATCH_SIZE # Reset metrics loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(NUM_CLASSES) log_string(str(datetime.now())) log_string('---- EPOCH %03d EVALUATION ----' % (EPOCH_CNT)) for batch_idx in range(num_batches): start_idx = batch_idx * BATCH_SIZE end_idx = (batch_idx + 1) * BATCH_SIZE batch_data, batch_label, batch_smpw = get_batch( TEST_DATASET, test_idxs, start_idx, end_idx) aug_data = provider.rotate_point_cloud(batch_data) feed_dict = { ops['pointclouds_pl']: aug_data, ops['labels_pl']: batch_label, ops['smpws_pl']: batch_smpw, ops['is_training_pl']: is_training } summary, step, loss_val, pred_val = sess.run( [ops['merged'], ops['step'], ops['loss'], ops['pred']], feed_dict=feed_dict) test_writer.add_summary(summary, step) pred_val = np.argmax(pred_val, 2) # BxN # Update metrics for i in range(len(pred_val)): for j in range(len(pred_val[i])): confusion_matrix.count_predicted(batch_label[i][j], pred_val[i][j]) loss_sum += loss_val iou_per_class = confusion_matrix.get_intersection_union_per_class() # Display metrics log_string('eval mean loss: %f' % (loss_sum / float(num_batches))) log_string("Overall accuracy : %f" % (confusion_matrix.get_overall_accuracy())) log_string("Average IoU : %f" % (confusion_matrix.get_average_intersection_union())) for i in range(1, NUM_CLASSES): log_string("IoU of %s : %f" % (data.LABELS_NAMES[i], iou_per_class[i])) EPOCH_CNT += 5 return confusion_matrix.get_overall_accuracy()
def train_one_epoch(sess, ops, train_writer): """Train one epoch Args: sess (tf.Session): the session to evaluate Tensors and ops ops (dict of tf.Operation): contain multiple operation mapped with with strings train_writer (tf.FileSaver): enable to log the training with TensorBoard """ is_training = True # Shuffle train samples train_idxs = np.arange(0, len(TRAIN_DATASET)) np.random.shuffle(train_idxs) num_batches = len(TRAIN_DATASET) / BATCH_SIZE log_string(str(datetime.now())) # Reset metrics loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(NUM_CLASSES) # Train over num_batches batches for batch_idx in range(num_batches): start_idx = batch_idx * BATCH_SIZE end_idx = (batch_idx + 1) * BATCH_SIZE batch_data, batch_label, batch_smpw = get_batch( TRAIN_DATASET, train_idxs, start_idx, end_idx, True, INPUT_DROPOUT) # Augment batched point clouds by z-axis rotation aug_data = provider.rotate_point_cloud(batch_data) # Get predicted labels feed_dict = { ops['pointclouds_pl']: aug_data, ops['labels_pl']: batch_label, ops['smpws_pl']: batch_smpw, 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'], ops['update_iou'] ], feed_dict=feed_dict) train_writer.add_summary(summary, step) pred_val = np.argmax(pred_val, 2) # Update metrics for i in range(len(pred_val)): for j in range(len(pred_val[i])): confusion_matrix.count_predicted(batch_label[i][j], pred_val[i][j]) loss_sum += loss_val # Every 10 batches, print metrics and reset them if (batch_idx + 1) % 10 == 0: log_string(' -- %03d / %03d --' % (batch_idx + 1, num_batches)) log_string('mean loss: %f' % (loss_sum / 10)) log_string("Overall accuracy : %f" % (confusion_matrix.get_overall_accuracy())) log_string("Average IoU : %f" % (confusion_matrix.get_average_intersection_union())) iou_per_class = confusion_matrix.get_intersection_union_per_class() for i in range(1, NUM_CLASSES): log_string("IoU of %s : %f" % (data.LABELS_NAMES[i], iou_per_class[i])) loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(NUM_CLASSES)
def eval_one_epoch(sess, ops, test_writer, stack): """Evaluate one epoch Args: sess (tf.Session): the session to evaluate tensors and operations ops (tf.Operation): the dict of operations test_writer (tf.summary.FileWriter): enable to log the evaluation on TensorBoard Returns: float: the overall accuracy computed on the test set """ global EPOCH_CNT is_training = False num_batches = TEST_DATASET.get_num_batches(BATCH_SIZE) # Reset metrics loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(NUM_CLASSES) log_string(str(datetime.now())) log_string('---- EPOCH %03d EVALUATION ----'%(EPOCH_CNT)) update_progress(0) for batch_idx in range(num_batches): progress = float(batch_idx)/float(num_batches) update_progress(round(progress,2)) batch_data, batch_label, batch_weights = stack.get() feed_dict = {ops['pointclouds_pl']: batch_data, ops['labels_pl']: batch_label, ops['smpws_pl']: batch_weights, ops['is_training_pl']: is_training} summary, step, loss_val, pred_val = sess.run([ops['merged'], ops['step'], ops['loss'], ops['pred']], feed_dict=feed_dict) test_writer.add_summary(summary, step) pred_val = np.argmax(pred_val, 2) # BxN # Update metrics for i in range(len(pred_val)): for j in range(len(pred_val[i])): confusion_matrix.count_predicted(batch_label[i][j], pred_val[i][j]) loss_sum += loss_val update_progress(1) iou_per_class = confusion_matrix.get_intersection_union_per_class() # Display metrics log_string('mean loss: %f' % (loss_sum / float(num_batches))) log_string("Overall accuracy : %f" %(confusion_matrix.get_overall_accuracy())) log_string("Average IoU : %f" %(confusion_matrix.get_average_intersection_union())) for i in range(1,NUM_CLASSES): log_string("IoU of %s : %f" % (TEST_DATASET.labels_names[i],iou_per_class[i])) EPOCH_CNT += 5 return confusion_matrix.get_overall_accuracy()
def train_one_epoch(sess, ops, train_writer): """Train one epoch Args: sess (tf.Session): the session to evaluate Tensors and ops ops (dict of tf.Operation): contain multiple operation mapped with with strings train_writer (tf.FileSaver): enable to log the training with TensorBoard """ is_training = True num_batches = int(TRAIN_DATASET.get_total_num_points() / (BATCH_SIZE * NUM_POINT)) * 2 log_string(str(datetime.now())) # Reset metrics loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(NUM_CLASSES) # Train over num_batches batches for batch_idx in range(num_batches): batch_data, batch_label, batch_weights = TRAIN_DATASET.next_batch( BATCH_SIZE, True, False) # Get predicted labels feed_dict = { ops['pointclouds_pl']: batch_data, ops['labels_pl']: batch_label, ops['smpws_pl']: batch_weights, 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'], ops['update_iou'] ], feed_dict=feed_dict) train_writer.add_summary(summary, step) pred_val = np.argmax(pred_val, 2) # Update metrics for i in range(len(pred_val)): for j in range(len(pred_val[i])): confusion_matrix.count_predicted(batch_label[i][j], pred_val[i][j]) loss_sum += loss_val # Every 10 batches, print metrics and reset them if (batch_idx + 1) % 10 == 0: log_string(' -- %03d / %03d --' % (batch_idx + 1, num_batches)) log_string('mean loss: %f' % (loss_sum / 10)) log_string("Overall accuracy : %f" % (confusion_matrix.get_overall_accuracy())) log_string("Average IoU : %f" % (confusion_matrix.get_average_intersection_union())) iou_per_class = confusion_matrix.get_intersection_union_per_class() for i in range(1, NUM_CLASSES): log_string("IoU of %s : %f" % (data.LABELS_NAMES[i], iou_per_class[i])) loss_sum = 0 confusion_matrix = metric.ConfusionMatrix(NUM_CLASSES)