# Copyright (c) Facebook, Inc. and its affiliates.
# 
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

""" Evaluation routine for 3D object detection with SUN RGB-D and ScanNet.
"""

import os
import sys
import numpy as np
from datetime import datetime
import argparse
import importlib
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = BASE_DIR
sys.path.append(os.path.join(ROOT_DIR, 'models'))
from ap_helper import APCalculator, parse_predictions, parse_groundtruths
from dump_helper import dump_results

parser = argparse.ArgumentParser()
parser.add_argument('--data_path', default='/scratch/cluster/yanght/Dataset/sunrgbd/', help='path to dataset')
parser.add_argument('--model', default='hdnet', help='Model file name [default: hdnet]')
parser.add_argument('--dataset', default='sunrgbd', help='Dataset name. sunrgbd or scannet. [default: sunrgbd]')
parser.add_argument('--checkpoint_path', default=None, help='Model checkpoint path [default: None]')
parser.add_argument('--dump_dir', default=None, help='Dump dir to save sample outputs [default: None]')
parser.add_argument('--num_point', type=int, default=20000, help='Point Number [default: 20000]')
parser.add_argument('--num_target', type=int, default=256, help='Point Number [default: 256]')
parser.add_argument('--batch_size', type=int, default=8, help='Batch Size during training [default: 8]')
parser.add_argument('--vote_factor', type=int, default=1, help='Number of votes generated from each seed [default: 1]')
parser.add_argument('--cluster_sampling', default='vote_fps', help='Sampling strategy for vote clusters: vote_fps, seed_fps, random [default: vote_fps]')
parser.add_argument('--ap_iou_thresh', type=float, default=0.25, help='AP IoU threshold [default: 0.25]')
parser.add_argument('--no_height', action='store_true', help='Do NOT use height signal in input.')
parser.add_argument('--use_color', action='store_true', help='Use RGB color in input.')
parser.add_argument('--use_sunrgbd_v2', action='store_true', help='Use SUN RGB-D V2 box labels.')
parser.add_argument('--use_3d_nms', action='store_true', help='Use 3D NMS instead of 2D NMS.')
parser.add_argument('--use_cls_nms', action='store_true', help='Use per class NMS.')
parser.add_argument('--use_old_type_nms', action='store_true', help='Use old type of NMS, IoBox2Area.')
parser.add_argument('--per_class_proposal', action='store_true', help='Duplicate each proposal num_class times.')
parser.add_argument('--nms_iou', type=float, default=0.25, help='NMS IoU threshold. [default: 0.25]')
parser.add_argument('--conf_thresh', type=float, default=0.05, help='Filter out predictions with obj prob less than it. [default: 0.05]')
parser.add_argument('--faster_eval', action='store_true', help='Faster evaluation by skippling empty bounding box removal.')
parser.add_argument('--shuffle_dataset', action='store_true', help='Shuffle the dataset (random order).')
parser.add_argument('--dump_results', action='store_true', help='Dump results.')

FLAGS = parser.parse_args()

if FLAGS.use_cls_nms:
    assert(FLAGS.use_3d_nms)

# ------------------------------------------------------------------------- GLOBAL CONFIG BEG
BATCH_SIZE = FLAGS.batch_size
NUM_POINT = FLAGS.num_point
DUMP_DIR = FLAGS.dump_dir
CHECKPOINT_PATH = FLAGS.checkpoint_path
assert(CHECKPOINT_PATH is not None)
FLAGS.DUMP_DIR = DUMP_DIR


# Prepare DUMP_DIR
if not os.path.exists(DUMP_DIR): os.mkdir(DUMP_DIR)
DUMP_FOUT = open(os.path.join(DUMP_DIR, 'log_eval.txt'), 'w')
DUMP_FOUT.write(str(FLAGS)+'\n')
def log_string(out_str):
    DUMP_FOUT.write(out_str+'\n')
    DUMP_FOUT.flush()
    print(out_str)

# Init datasets and dataloaders 
def my_worker_init_fn(worker_id):
    np.random.seed(np.random.get_state()[1][0] + worker_id)

if FLAGS.dataset == 'sunrgbd':
    sys.path.append(os.path.join(ROOT_DIR, 'sunrgbd'))
    from sunrgbd_detection_dataset_hd import SunrgbdDetectionVotesDataset, MAX_NUM_OBJ
    from model_util_sunrgbd import SunrgbdDatasetConfig
    DATASET_CONFIG = SunrgbdDatasetConfig()
    TEST_DATASET = SunrgbdDetectionVotesDataset(FLAGS.data_path, 'val', num_points=NUM_POINT,
        augment=False, use_color=FLAGS.use_color, use_height=(not FLAGS.no_height),
        use_v1=(not FLAGS.use_sunrgbd_v2))
elif FLAGS.dataset == 'scannet':
    sys.path.append(os.path.join(ROOT_DIR, 'scannet'))
    from scannet_detection_dataset_hd import ScannetDetectionDataset, MAX_NUM_OBJ
    from model_util_scannet import ScannetDatasetConfig
    DATASET_CONFIG = ScannetDatasetConfig()
    TEST_DATASET = ScannetDetectionDataset(FLAGS.data_path, 'val', num_points=NUM_POINT,
                                           augment=False, use_angle=False,
                                           use_color=FLAGS.use_color, use_height=(not FLAGS.no_height))
else:
    print('Unknown dataset %s. Exiting...'%(FLAGS.dataset))
    exit(-1)
print(len(TEST_DATASET))
TEST_DATALOADER = DataLoader(TEST_DATASET, batch_size=BATCH_SIZE,
    shuffle=FLAGS.shuffle_dataset, num_workers=4, worker_init_fn=my_worker_init_fn)

# Init the model and optimzier
MODEL = importlib.import_module(FLAGS.model) # import network module
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
num_input_channel = int(FLAGS.use_color)*3 + int(not FLAGS.no_height)*1

Detector = MODEL.HDNet

net = Detector(num_class=DATASET_CONFIG.num_class,
               num_heading_bin=DATASET_CONFIG.num_heading_bin,
               num_size_cluster=DATASET_CONFIG.num_size_cluster,
               mean_size_arr=DATASET_CONFIG.mean_size_arr,
               num_proposal=FLAGS.num_target,
               input_feature_dim=num_input_channel,
               vote_factor=FLAGS.vote_factor,
               sampling=FLAGS.cluster_sampling)

if torch.cuda.device_count() > 1:
    log_string("Let's use %d GPUs!" % (torch.cuda.device_count()))
    # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
    net = nn.DataParallel(net) 
net.to(device)
criterion = MODEL.get_loss

# Load the Adam optimizer
optimizer = optim.Adam(net.parameters(), lr=0.001)

# Load checkpoint if there is any
if CHECKPOINT_PATH is not None and os.path.isfile(CHECKPOINT_PATH):
    checkpoint = torch.load(CHECKPOINT_PATH)
    checkpoint_multigpu = dict()
    if torch.cuda.device_count() > 1:
        for name, param in checkpoint['model_state_dict'].items():
            checkpoint_multigpu.update({'module.' + name: param})
        net.load_state_dict(checkpoint_multigpu)
    else:
        net.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    log_string("Loaded checkpoint %s (epoch: %d)"%(CHECKPOINT_PATH, epoch))

# Used for AP calculation
CONFIG_DICT = {'remove_empty_box':False, 'use_3d_nms':True,
    'nms_iou':0.25, 'use_old_type_nms':False, 'cls_nms':True,
    'per_class_proposal': True, 'conf_thresh':0.05,
    'dataset_config':DATASET_CONFIG}

CONFIG_DICT_L = {'remove_empty_box':False, 'use_3d_nms':True,
    'nms_iou':0.5, 'use_old_type_nms':False, 'cls_nms':True,
    'per_class_proposal': True, 'conf_thresh':0.05,
    'dataset_config':DATASET_CONFIG}

# ------------------------------------------------------------------------- GLOBAL CONFIG END

def evaluate_one_epoch():
    stat_dict = {}

    ap_calculator = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh,
        class2type_map=DATASET_CONFIG.class2type)
    ap_calculator_l = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh*2,
        class2type_map=DATASET_CONFIG.class2type)

    net.eval() # set model to eval mode (for bn and dp)
    for batch_idx, batch_data_label in enumerate(TEST_DATALOADER):
        end_points = {}
        if batch_idx % 10 == 0:
            print('Eval batch: %d'%(batch_idx))
        for key in batch_data_label:
            batch_data_label[key] = batch_data_label[key].to(device)
        
        # Forward pass
        inputs = {'point_clouds': batch_data_label['point_clouds']}
        with torch.no_grad():
            end_points = net(inputs, end_points)

        # Compute loss
        for key in batch_data_label:
            end_points[key] = batch_data_label[key]
        loss, end_points = criterion(inputs, end_points, DATASET_CONFIG)

        # Accumulate statistics and print out
        for key in end_points:
            if 'loss' in key or 'acc' in key or 'ratio' in key:
                if key not in stat_dict: stat_dict[key] = 0
                stat_dict[key] += end_points[key].item()

        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT, opt_ang=(FLAGS.dataset == 'sunrgbd'))
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT) 
        ap_calculator.step(batch_pred_map_cls, batch_gt_map_cls)

        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT_L, opt_ang=(FLAGS.dataset == 'sunrgbd'))
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT_L) 
        ap_calculator_l.step(batch_pred_map_cls, batch_gt_map_cls)
        
        if FLAGS.dump_results:
            dump_results(end_points, DUMP_DIR+'/result/', DATASET_CONFIG, TEST_DATASET, opt_ang=(FLAGS.dataset == 'sunrgbd'))


    # Log statistics
    for key in sorted(stat_dict.keys()):
        log_string('eval mean %s: %f'%(key, stat_dict[key]/(float(batch_idx+1))))

    metrics_dict = ap_calculator.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.25, eval %s: %f'%(key, metrics_dict[key]))
    metrics_dict = ap_calculator_l.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.5, eval %s: %f'%(key, metrics_dict[key]))

    mean_loss = stat_dict['loss']/float(batch_idx+1)
    return mean_loss


def eval():
    log_string(str(datetime.now()))
    # Reset numpy seed.
    # REF: https://github.com/pytorch/pytorch/issues/5059
    np.random.seed()
    loss = evaluate_one_epoch()

if __name__=='__main__':
    eval()

47dacbbafc8d1e468633d15b73ff2f67fcbe1076
<mask token>


def log_string(out_str):
    DUMP_FOUT.write(out_str + '\n')
    DUMP_FOUT.flush()
    print(out_str)


<mask token>


def eval():
    log_string(str(datetime.now()))
    np.random.seed()
    loss = evaluate_one_epoch()


<mask token>

<mask token>
sys.path.append(os.path.join(ROOT_DIR, 'models'))
<mask token>
parser.add_argument('--data_path', default=
    '/scratch/cluster/yanght/Dataset/sunrgbd/', help='path to dataset')
parser.add_argument('--model', default='hdnet', help=
    'Model file name [default: hdnet]')
parser.add_argument('--dataset', default='sunrgbd', help=
    'Dataset name. sunrgbd or scannet. [default: sunrgbd]')
parser.add_argument('--checkpoint_path', default=None, help=
    'Model checkpoint path [default: None]')
parser.add_argument('--dump_dir', default=None, help=
    'Dump dir to save sample outputs [default: None]')
parser.add_argument('--num_point', type=int, default=20000, help=
    'Point Number [default: 20000]')
parser.add_argument('--num_target', type=int, default=256, help=
    'Point Number [default: 256]')
parser.add_argument('--batch_size', type=int, default=8, help=
    'Batch Size during training [default: 8]')
parser.add_argument('--vote_factor', type=int, default=1, help=
    'Number of votes generated from each seed [default: 1]')
parser.add_argument('--cluster_sampling', default='vote_fps', help=
    'Sampling strategy for vote clusters: vote_fps, seed_fps, random [default: vote_fps]'
    )
parser.add_argument('--ap_iou_thresh', type=float, default=0.25, help=
    'AP IoU threshold [default: 0.25]')
parser.add_argument('--no_height', action='store_true', help=
    'Do NOT use height signal in input.')
parser.add_argument('--use_color', action='store_true', help=
    'Use RGB color in input.')
parser.add_argument('--use_sunrgbd_v2', action='store_true', help=
    'Use SUN RGB-D V2 box labels.')
parser.add_argument('--use_3d_nms', action='store_true', help=
    'Use 3D NMS instead of 2D NMS.')
parser.add_argument('--use_cls_nms', action='store_true', help=
    'Use per class NMS.')
parser.add_argument('--use_old_type_nms', action='store_true', help=
    'Use old type of NMS, IoBox2Area.')
parser.add_argument('--per_class_proposal', action='store_true', help=
    'Duplicate each proposal num_class times.')
parser.add_argument('--nms_iou', type=float, default=0.25, help=
    'NMS IoU threshold. [default: 0.25]')
parser.add_argument('--conf_thresh', type=float, default=0.05, help=
    'Filter out predictions with obj prob less than it. [default: 0.05]')
parser.add_argument('--faster_eval', action='store_true', help=
    'Faster evaluation by skippling empty bounding box removal.')
parser.add_argument('--shuffle_dataset', action='store_true', help=
    'Shuffle the dataset (random order).')
parser.add_argument('--dump_results', action='store_true', help='Dump results.'
    )
<mask token>
if FLAGS.use_cls_nms:
    assert FLAGS.use_3d_nms
<mask token>
assert CHECKPOINT_PATH is not None
<mask token>
if not os.path.exists(DUMP_DIR):
    os.mkdir(DUMP_DIR)
<mask token>
DUMP_FOUT.write(str(FLAGS) + '\n')


def log_string(out_str):
    DUMP_FOUT.write(out_str + '\n')
    DUMP_FOUT.flush()
    print(out_str)


def my_worker_init_fn(worker_id):
    np.random.seed(np.random.get_state()[1][0] + worker_id)


if FLAGS.dataset == 'sunrgbd':
    sys.path.append(os.path.join(ROOT_DIR, 'sunrgbd'))
    from sunrgbd_detection_dataset_hd import SunrgbdDetectionVotesDataset, MAX_NUM_OBJ
    from model_util_sunrgbd import SunrgbdDatasetConfig
    DATASET_CONFIG = SunrgbdDatasetConfig()
    TEST_DATASET = SunrgbdDetectionVotesDataset(FLAGS.data_path, 'val',
        num_points=NUM_POINT, augment=False, use_color=FLAGS.use_color,
        use_height=not FLAGS.no_height, use_v1=not FLAGS.use_sunrgbd_v2)
elif FLAGS.dataset == 'scannet':
    sys.path.append(os.path.join(ROOT_DIR, 'scannet'))
    from scannet_detection_dataset_hd import ScannetDetectionDataset, MAX_NUM_OBJ
    from model_util_scannet import ScannetDatasetConfig
    DATASET_CONFIG = ScannetDatasetConfig()
    TEST_DATASET = ScannetDetectionDataset(FLAGS.data_path, 'val',
        num_points=NUM_POINT, augment=False, use_angle=False, use_color=
        FLAGS.use_color, use_height=not FLAGS.no_height)
else:
    print('Unknown dataset %s. Exiting...' % FLAGS.dataset)
    exit(-1)
print(len(TEST_DATASET))
<mask token>
if torch.cuda.device_count() > 1:
    log_string("Let's use %d GPUs!" % torch.cuda.device_count())
    net = nn.DataParallel(net)
net.to(device)
<mask token>
if CHECKPOINT_PATH is not None and os.path.isfile(CHECKPOINT_PATH):
    checkpoint = torch.load(CHECKPOINT_PATH)
    checkpoint_multigpu = dict()
    if torch.cuda.device_count() > 1:
        for name, param in checkpoint['model_state_dict'].items():
            checkpoint_multigpu.update({('module.' + name): param})
        net.load_state_dict(checkpoint_multigpu)
    else:
        net.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    log_string('Loaded checkpoint %s (epoch: %d)' % (CHECKPOINT_PATH, epoch))
<mask token>


def evaluate_one_epoch():
    stat_dict = {}
    ap_calculator = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh,
        class2type_map=DATASET_CONFIG.class2type)
    ap_calculator_l = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh * 2,
        class2type_map=DATASET_CONFIG.class2type)
    net.eval()
    for batch_idx, batch_data_label in enumerate(TEST_DATALOADER):
        end_points = {}
        if batch_idx % 10 == 0:
            print('Eval batch: %d' % batch_idx)
        for key in batch_data_label:
            batch_data_label[key] = batch_data_label[key].to(device)
        inputs = {'point_clouds': batch_data_label['point_clouds']}
        with torch.no_grad():
            end_points = net(inputs, end_points)
        for key in batch_data_label:
            end_points[key] = batch_data_label[key]
        loss, end_points = criterion(inputs, end_points, DATASET_CONFIG)
        for key in end_points:
            if 'loss' in key or 'acc' in key or 'ratio' in key:
                if key not in stat_dict:
                    stat_dict[key] = 0
                stat_dict[key] += end_points[key].item()
        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT,
            opt_ang=FLAGS.dataset == 'sunrgbd')
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT)
        ap_calculator.step(batch_pred_map_cls, batch_gt_map_cls)
        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT_L,
            opt_ang=FLAGS.dataset == 'sunrgbd')
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT_L)
        ap_calculator_l.step(batch_pred_map_cls, batch_gt_map_cls)
        if FLAGS.dump_results:
            dump_results(end_points, DUMP_DIR + '/result/', DATASET_CONFIG,
                TEST_DATASET, opt_ang=FLAGS.dataset == 'sunrgbd')
    for key in sorted(stat_dict.keys()):
        log_string('eval mean %s: %f' % (key, stat_dict[key] / float(
            batch_idx + 1)))
    metrics_dict = ap_calculator.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.25, eval %s: %f' % (key, metrics_dict[key]))
    metrics_dict = ap_calculator_l.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.5, eval %s: %f' % (key, metrics_dict[key]))
    mean_loss = stat_dict['loss'] / float(batch_idx + 1)
    return mean_loss


def eval():
    log_string(str(datetime.now()))
    np.random.seed()
    loss = evaluate_one_epoch()


if __name__ == '__main__':
    eval()

<mask token>
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = BASE_DIR
sys.path.append(os.path.join(ROOT_DIR, 'models'))
<mask token>
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', default=
    '/scratch/cluster/yanght/Dataset/sunrgbd/', help='path to dataset')
parser.add_argument('--model', default='hdnet', help=
    'Model file name [default: hdnet]')
parser.add_argument('--dataset', default='sunrgbd', help=
    'Dataset name. sunrgbd or scannet. [default: sunrgbd]')
parser.add_argument('--checkpoint_path', default=None, help=
    'Model checkpoint path [default: None]')
parser.add_argument('--dump_dir', default=None, help=
    'Dump dir to save sample outputs [default: None]')
parser.add_argument('--num_point', type=int, default=20000, help=
    'Point Number [default: 20000]')
parser.add_argument('--num_target', type=int, default=256, help=
    'Point Number [default: 256]')
parser.add_argument('--batch_size', type=int, default=8, help=
    'Batch Size during training [default: 8]')
parser.add_argument('--vote_factor', type=int, default=1, help=
    'Number of votes generated from each seed [default: 1]')
parser.add_argument('--cluster_sampling', default='vote_fps', help=
    'Sampling strategy for vote clusters: vote_fps, seed_fps, random [default: vote_fps]'
    )
parser.add_argument('--ap_iou_thresh', type=float, default=0.25, help=
    'AP IoU threshold [default: 0.25]')
parser.add_argument('--no_height', action='store_true', help=
    'Do NOT use height signal in input.')
parser.add_argument('--use_color', action='store_true', help=
    'Use RGB color in input.')
parser.add_argument('--use_sunrgbd_v2', action='store_true', help=
    'Use SUN RGB-D V2 box labels.')
parser.add_argument('--use_3d_nms', action='store_true', help=
    'Use 3D NMS instead of 2D NMS.')
parser.add_argument('--use_cls_nms', action='store_true', help=
    'Use per class NMS.')
parser.add_argument('--use_old_type_nms', action='store_true', help=
    'Use old type of NMS, IoBox2Area.')
parser.add_argument('--per_class_proposal', action='store_true', help=
    'Duplicate each proposal num_class times.')
parser.add_argument('--nms_iou', type=float, default=0.25, help=
    'NMS IoU threshold. [default: 0.25]')
parser.add_argument('--conf_thresh', type=float, default=0.05, help=
    'Filter out predictions with obj prob less than it. [default: 0.05]')
parser.add_argument('--faster_eval', action='store_true', help=
    'Faster evaluation by skippling empty bounding box removal.')
parser.add_argument('--shuffle_dataset', action='store_true', help=
    'Shuffle the dataset (random order).')
parser.add_argument('--dump_results', action='store_true', help='Dump results.'
    )
FLAGS = parser.parse_args()
if FLAGS.use_cls_nms:
    assert FLAGS.use_3d_nms
BATCH_SIZE = FLAGS.batch_size
NUM_POINT = FLAGS.num_point
DUMP_DIR = FLAGS.dump_dir
CHECKPOINT_PATH = FLAGS.checkpoint_path
assert CHECKPOINT_PATH is not None
FLAGS.DUMP_DIR = DUMP_DIR
if not os.path.exists(DUMP_DIR):
    os.mkdir(DUMP_DIR)
DUMP_FOUT = open(os.path.join(DUMP_DIR, 'log_eval.txt'), 'w')
DUMP_FOUT.write(str(FLAGS) + '\n')


def log_string(out_str):
    DUMP_FOUT.write(out_str + '\n')
    DUMP_FOUT.flush()
    print(out_str)


def my_worker_init_fn(worker_id):
    np.random.seed(np.random.get_state()[1][0] + worker_id)


if FLAGS.dataset == 'sunrgbd':
    sys.path.append(os.path.join(ROOT_DIR, 'sunrgbd'))
    from sunrgbd_detection_dataset_hd import SunrgbdDetectionVotesDataset, MAX_NUM_OBJ
    from model_util_sunrgbd import SunrgbdDatasetConfig
    DATASET_CONFIG = SunrgbdDatasetConfig()
    TEST_DATASET = SunrgbdDetectionVotesDataset(FLAGS.data_path, 'val',
        num_points=NUM_POINT, augment=False, use_color=FLAGS.use_color,
        use_height=not FLAGS.no_height, use_v1=not FLAGS.use_sunrgbd_v2)
elif FLAGS.dataset == 'scannet':
    sys.path.append(os.path.join(ROOT_DIR, 'scannet'))
    from scannet_detection_dataset_hd import ScannetDetectionDataset, MAX_NUM_OBJ
    from model_util_scannet import ScannetDatasetConfig
    DATASET_CONFIG = ScannetDatasetConfig()
    TEST_DATASET = ScannetDetectionDataset(FLAGS.data_path, 'val',
        num_points=NUM_POINT, augment=False, use_angle=False, use_color=
        FLAGS.use_color, use_height=not FLAGS.no_height)
else:
    print('Unknown dataset %s. Exiting...' % FLAGS.dataset)
    exit(-1)
print(len(TEST_DATASET))
TEST_DATALOADER = DataLoader(TEST_DATASET, batch_size=BATCH_SIZE, shuffle=
    FLAGS.shuffle_dataset, num_workers=4, worker_init_fn=my_worker_init_fn)
MODEL = importlib.import_module(FLAGS.model)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
num_input_channel = int(FLAGS.use_color) * 3 + int(not FLAGS.no_height) * 1
Detector = MODEL.HDNet
net = Detector(num_class=DATASET_CONFIG.num_class, num_heading_bin=
    DATASET_CONFIG.num_heading_bin, num_size_cluster=DATASET_CONFIG.
    num_size_cluster, mean_size_arr=DATASET_CONFIG.mean_size_arr,
    num_proposal=FLAGS.num_target, input_feature_dim=num_input_channel,
    vote_factor=FLAGS.vote_factor, sampling=FLAGS.cluster_sampling)
if torch.cuda.device_count() > 1:
    log_string("Let's use %d GPUs!" % torch.cuda.device_count())
    net = nn.DataParallel(net)
net.to(device)
criterion = MODEL.get_loss
optimizer = optim.Adam(net.parameters(), lr=0.001)
if CHECKPOINT_PATH is not None and os.path.isfile(CHECKPOINT_PATH):
    checkpoint = torch.load(CHECKPOINT_PATH)
    checkpoint_multigpu = dict()
    if torch.cuda.device_count() > 1:
        for name, param in checkpoint['model_state_dict'].items():
            checkpoint_multigpu.update({('module.' + name): param})
        net.load_state_dict(checkpoint_multigpu)
    else:
        net.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    log_string('Loaded checkpoint %s (epoch: %d)' % (CHECKPOINT_PATH, epoch))
CONFIG_DICT = {'remove_empty_box': False, 'use_3d_nms': True, 'nms_iou': 
    0.25, 'use_old_type_nms': False, 'cls_nms': True, 'per_class_proposal':
    True, 'conf_thresh': 0.05, 'dataset_config': DATASET_CONFIG}
CONFIG_DICT_L = {'remove_empty_box': False, 'use_3d_nms': True, 'nms_iou': 
    0.5, 'use_old_type_nms': False, 'cls_nms': True, 'per_class_proposal': 
    True, 'conf_thresh': 0.05, 'dataset_config': DATASET_CONFIG}


def evaluate_one_epoch():
    stat_dict = {}
    ap_calculator = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh,
        class2type_map=DATASET_CONFIG.class2type)
    ap_calculator_l = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh * 2,
        class2type_map=DATASET_CONFIG.class2type)
    net.eval()
    for batch_idx, batch_data_label in enumerate(TEST_DATALOADER):
        end_points = {}
        if batch_idx % 10 == 0:
            print('Eval batch: %d' % batch_idx)
        for key in batch_data_label:
            batch_data_label[key] = batch_data_label[key].to(device)
        inputs = {'point_clouds': batch_data_label['point_clouds']}
        with torch.no_grad():
            end_points = net(inputs, end_points)
        for key in batch_data_label:
            end_points[key] = batch_data_label[key]
        loss, end_points = criterion(inputs, end_points, DATASET_CONFIG)
        for key in end_points:
            if 'loss' in key or 'acc' in key or 'ratio' in key:
                if key not in stat_dict:
                    stat_dict[key] = 0
                stat_dict[key] += end_points[key].item()
        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT,
            opt_ang=FLAGS.dataset == 'sunrgbd')
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT)
        ap_calculator.step(batch_pred_map_cls, batch_gt_map_cls)
        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT_L,
            opt_ang=FLAGS.dataset == 'sunrgbd')
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT_L)
        ap_calculator_l.step(batch_pred_map_cls, batch_gt_map_cls)
        if FLAGS.dump_results:
            dump_results(end_points, DUMP_DIR + '/result/', DATASET_CONFIG,
                TEST_DATASET, opt_ang=FLAGS.dataset == 'sunrgbd')
    for key in sorted(stat_dict.keys()):
        log_string('eval mean %s: %f' % (key, stat_dict[key] / float(
            batch_idx + 1)))
    metrics_dict = ap_calculator.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.25, eval %s: %f' % (key, metrics_dict[key]))
    metrics_dict = ap_calculator_l.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.5, eval %s: %f' % (key, metrics_dict[key]))
    mean_loss = stat_dict['loss'] / float(batch_idx + 1)
    return mean_loss


def eval():
    log_string(str(datetime.now()))
    np.random.seed()
    loss = evaluate_one_epoch()


if __name__ == '__main__':
    eval()

<mask token>
import os
import sys
import numpy as np
from datetime import datetime
import argparse
import importlib
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = BASE_DIR
sys.path.append(os.path.join(ROOT_DIR, 'models'))
from ap_helper import APCalculator, parse_predictions, parse_groundtruths
from dump_helper import dump_results
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', default=
    '/scratch/cluster/yanght/Dataset/sunrgbd/', help='path to dataset')
parser.add_argument('--model', default='hdnet', help=
    'Model file name [default: hdnet]')
parser.add_argument('--dataset', default='sunrgbd', help=
    'Dataset name. sunrgbd or scannet. [default: sunrgbd]')
parser.add_argument('--checkpoint_path', default=None, help=
    'Model checkpoint path [default: None]')
parser.add_argument('--dump_dir', default=None, help=
    'Dump dir to save sample outputs [default: None]')
parser.add_argument('--num_point', type=int, default=20000, help=
    'Point Number [default: 20000]')
parser.add_argument('--num_target', type=int, default=256, help=
    'Point Number [default: 256]')
parser.add_argument('--batch_size', type=int, default=8, help=
    'Batch Size during training [default: 8]')
parser.add_argument('--vote_factor', type=int, default=1, help=
    'Number of votes generated from each seed [default: 1]')
parser.add_argument('--cluster_sampling', default='vote_fps', help=
    'Sampling strategy for vote clusters: vote_fps, seed_fps, random [default: vote_fps]'
    )
parser.add_argument('--ap_iou_thresh', type=float, default=0.25, help=
    'AP IoU threshold [default: 0.25]')
parser.add_argument('--no_height', action='store_true', help=
    'Do NOT use height signal in input.')
parser.add_argument('--use_color', action='store_true', help=
    'Use RGB color in input.')
parser.add_argument('--use_sunrgbd_v2', action='store_true', help=
    'Use SUN RGB-D V2 box labels.')
parser.add_argument('--use_3d_nms', action='store_true', help=
    'Use 3D NMS instead of 2D NMS.')
parser.add_argument('--use_cls_nms', action='store_true', help=
    'Use per class NMS.')
parser.add_argument('--use_old_type_nms', action='store_true', help=
    'Use old type of NMS, IoBox2Area.')
parser.add_argument('--per_class_proposal', action='store_true', help=
    'Duplicate each proposal num_class times.')
parser.add_argument('--nms_iou', type=float, default=0.25, help=
    'NMS IoU threshold. [default: 0.25]')
parser.add_argument('--conf_thresh', type=float, default=0.05, help=
    'Filter out predictions with obj prob less than it. [default: 0.05]')
parser.add_argument('--faster_eval', action='store_true', help=
    'Faster evaluation by skippling empty bounding box removal.')
parser.add_argument('--shuffle_dataset', action='store_true', help=
    'Shuffle the dataset (random order).')
parser.add_argument('--dump_results', action='store_true', help='Dump results.'
    )
FLAGS = parser.parse_args()
if FLAGS.use_cls_nms:
    assert FLAGS.use_3d_nms
BATCH_SIZE = FLAGS.batch_size
NUM_POINT = FLAGS.num_point
DUMP_DIR = FLAGS.dump_dir
CHECKPOINT_PATH = FLAGS.checkpoint_path
assert CHECKPOINT_PATH is not None
FLAGS.DUMP_DIR = DUMP_DIR
if not os.path.exists(DUMP_DIR):
    os.mkdir(DUMP_DIR)
DUMP_FOUT = open(os.path.join(DUMP_DIR, 'log_eval.txt'), 'w')
DUMP_FOUT.write(str(FLAGS) + '\n')


def log_string(out_str):
    DUMP_FOUT.write(out_str + '\n')
    DUMP_FOUT.flush()
    print(out_str)


def my_worker_init_fn(worker_id):
    np.random.seed(np.random.get_state()[1][0] + worker_id)


if FLAGS.dataset == 'sunrgbd':
    sys.path.append(os.path.join(ROOT_DIR, 'sunrgbd'))
    from sunrgbd_detection_dataset_hd import SunrgbdDetectionVotesDataset, MAX_NUM_OBJ
    from model_util_sunrgbd import SunrgbdDatasetConfig
    DATASET_CONFIG = SunrgbdDatasetConfig()
    TEST_DATASET = SunrgbdDetectionVotesDataset(FLAGS.data_path, 'val',
        num_points=NUM_POINT, augment=False, use_color=FLAGS.use_color,
        use_height=not FLAGS.no_height, use_v1=not FLAGS.use_sunrgbd_v2)
elif FLAGS.dataset == 'scannet':
    sys.path.append(os.path.join(ROOT_DIR, 'scannet'))
    from scannet_detection_dataset_hd import ScannetDetectionDataset, MAX_NUM_OBJ
    from model_util_scannet import ScannetDatasetConfig
    DATASET_CONFIG = ScannetDatasetConfig()
    TEST_DATASET = ScannetDetectionDataset(FLAGS.data_path, 'val',
        num_points=NUM_POINT, augment=False, use_angle=False, use_color=
        FLAGS.use_color, use_height=not FLAGS.no_height)
else:
    print('Unknown dataset %s. Exiting...' % FLAGS.dataset)
    exit(-1)
print(len(TEST_DATASET))
TEST_DATALOADER = DataLoader(TEST_DATASET, batch_size=BATCH_SIZE, shuffle=
    FLAGS.shuffle_dataset, num_workers=4, worker_init_fn=my_worker_init_fn)
MODEL = importlib.import_module(FLAGS.model)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
num_input_channel = int(FLAGS.use_color) * 3 + int(not FLAGS.no_height) * 1
Detector = MODEL.HDNet
net = Detector(num_class=DATASET_CONFIG.num_class, num_heading_bin=
    DATASET_CONFIG.num_heading_bin, num_size_cluster=DATASET_CONFIG.
    num_size_cluster, mean_size_arr=DATASET_CONFIG.mean_size_arr,
    num_proposal=FLAGS.num_target, input_feature_dim=num_input_channel,
    vote_factor=FLAGS.vote_factor, sampling=FLAGS.cluster_sampling)
if torch.cuda.device_count() > 1:
    log_string("Let's use %d GPUs!" % torch.cuda.device_count())
    net = nn.DataParallel(net)
net.to(device)
criterion = MODEL.get_loss
optimizer = optim.Adam(net.parameters(), lr=0.001)
if CHECKPOINT_PATH is not None and os.path.isfile(CHECKPOINT_PATH):
    checkpoint = torch.load(CHECKPOINT_PATH)
    checkpoint_multigpu = dict()
    if torch.cuda.device_count() > 1:
        for name, param in checkpoint['model_state_dict'].items():
            checkpoint_multigpu.update({('module.' + name): param})
        net.load_state_dict(checkpoint_multigpu)
    else:
        net.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    log_string('Loaded checkpoint %s (epoch: %d)' % (CHECKPOINT_PATH, epoch))
CONFIG_DICT = {'remove_empty_box': False, 'use_3d_nms': True, 'nms_iou': 
    0.25, 'use_old_type_nms': False, 'cls_nms': True, 'per_class_proposal':
    True, 'conf_thresh': 0.05, 'dataset_config': DATASET_CONFIG}
CONFIG_DICT_L = {'remove_empty_box': False, 'use_3d_nms': True, 'nms_iou': 
    0.5, 'use_old_type_nms': False, 'cls_nms': True, 'per_class_proposal': 
    True, 'conf_thresh': 0.05, 'dataset_config': DATASET_CONFIG}


def evaluate_one_epoch():
    stat_dict = {}
    ap_calculator = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh,
        class2type_map=DATASET_CONFIG.class2type)
    ap_calculator_l = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh * 2,
        class2type_map=DATASET_CONFIG.class2type)
    net.eval()
    for batch_idx, batch_data_label in enumerate(TEST_DATALOADER):
        end_points = {}
        if batch_idx % 10 == 0:
            print('Eval batch: %d' % batch_idx)
        for key in batch_data_label:
            batch_data_label[key] = batch_data_label[key].to(device)
        inputs = {'point_clouds': batch_data_label['point_clouds']}
        with torch.no_grad():
            end_points = net(inputs, end_points)
        for key in batch_data_label:
            end_points[key] = batch_data_label[key]
        loss, end_points = criterion(inputs, end_points, DATASET_CONFIG)
        for key in end_points:
            if 'loss' in key or 'acc' in key or 'ratio' in key:
                if key not in stat_dict:
                    stat_dict[key] = 0
                stat_dict[key] += end_points[key].item()
        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT,
            opt_ang=FLAGS.dataset == 'sunrgbd')
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT)
        ap_calculator.step(batch_pred_map_cls, batch_gt_map_cls)
        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT_L,
            opt_ang=FLAGS.dataset == 'sunrgbd')
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT_L)
        ap_calculator_l.step(batch_pred_map_cls, batch_gt_map_cls)
        if FLAGS.dump_results:
            dump_results(end_points, DUMP_DIR + '/result/', DATASET_CONFIG,
                TEST_DATASET, opt_ang=FLAGS.dataset == 'sunrgbd')
    for key in sorted(stat_dict.keys()):
        log_string('eval mean %s: %f' % (key, stat_dict[key] / float(
            batch_idx + 1)))
    metrics_dict = ap_calculator.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.25, eval %s: %f' % (key, metrics_dict[key]))
    metrics_dict = ap_calculator_l.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.5, eval %s: %f' % (key, metrics_dict[key]))
    mean_loss = stat_dict['loss'] / float(batch_idx + 1)
    return mean_loss


def eval():
    log_string(str(datetime.now()))
    np.random.seed()
    loss = evaluate_one_epoch()


if __name__ == '__main__':
    eval()

# Copyright (c) Facebook, Inc. and its affiliates.
# 
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

""" Evaluation routine for 3D object detection with SUN RGB-D and ScanNet.
"""

import os
import sys
import numpy as np
from datetime import datetime
import argparse
import importlib
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = BASE_DIR
sys.path.append(os.path.join(ROOT_DIR, 'models'))
from ap_helper import APCalculator, parse_predictions, parse_groundtruths
from dump_helper import dump_results

parser = argparse.ArgumentParser()
parser.add_argument('--data_path', default='/scratch/cluster/yanght/Dataset/sunrgbd/', help='path to dataset')
parser.add_argument('--model', default='hdnet', help='Model file name [default: hdnet]')
parser.add_argument('--dataset', default='sunrgbd', help='Dataset name. sunrgbd or scannet. [default: sunrgbd]')
parser.add_argument('--checkpoint_path', default=None, help='Model checkpoint path [default: None]')
parser.add_argument('--dump_dir', default=None, help='Dump dir to save sample outputs [default: None]')
parser.add_argument('--num_point', type=int, default=20000, help='Point Number [default: 20000]')
parser.add_argument('--num_target', type=int, default=256, help='Point Number [default: 256]')
parser.add_argument('--batch_size', type=int, default=8, help='Batch Size during training [default: 8]')
parser.add_argument('--vote_factor', type=int, default=1, help='Number of votes generated from each seed [default: 1]')
parser.add_argument('--cluster_sampling', default='vote_fps', help='Sampling strategy for vote clusters: vote_fps, seed_fps, random [default: vote_fps]')
parser.add_argument('--ap_iou_thresh', type=float, default=0.25, help='AP IoU threshold [default: 0.25]')
parser.add_argument('--no_height', action='store_true', help='Do NOT use height signal in input.')
parser.add_argument('--use_color', action='store_true', help='Use RGB color in input.')
parser.add_argument('--use_sunrgbd_v2', action='store_true', help='Use SUN RGB-D V2 box labels.')
parser.add_argument('--use_3d_nms', action='store_true', help='Use 3D NMS instead of 2D NMS.')
parser.add_argument('--use_cls_nms', action='store_true', help='Use per class NMS.')
parser.add_argument('--use_old_type_nms', action='store_true', help='Use old type of NMS, IoBox2Area.')
parser.add_argument('--per_class_proposal', action='store_true', help='Duplicate each proposal num_class times.')
parser.add_argument('--nms_iou', type=float, default=0.25, help='NMS IoU threshold. [default: 0.25]')
parser.add_argument('--conf_thresh', type=float, default=0.05, help='Filter out predictions with obj prob less than it. [default: 0.05]')
parser.add_argument('--faster_eval', action='store_true', help='Faster evaluation by skippling empty bounding box removal.')
parser.add_argument('--shuffle_dataset', action='store_true', help='Shuffle the dataset (random order).')
parser.add_argument('--dump_results', action='store_true', help='Dump results.')

FLAGS = parser.parse_args()

if FLAGS.use_cls_nms:
    assert(FLAGS.use_3d_nms)

# ------------------------------------------------------------------------- GLOBAL CONFIG BEG
BATCH_SIZE = FLAGS.batch_size
NUM_POINT = FLAGS.num_point
DUMP_DIR = FLAGS.dump_dir
CHECKPOINT_PATH = FLAGS.checkpoint_path
assert(CHECKPOINT_PATH is not None)
FLAGS.DUMP_DIR = DUMP_DIR


# Prepare DUMP_DIR
if not os.path.exists(DUMP_DIR): os.mkdir(DUMP_DIR)
DUMP_FOUT = open(os.path.join(DUMP_DIR, 'log_eval.txt'), 'w')
DUMP_FOUT.write(str(FLAGS)+'\n')
def log_string(out_str):
    DUMP_FOUT.write(out_str+'\n')
    DUMP_FOUT.flush()
    print(out_str)

# Init datasets and dataloaders 
def my_worker_init_fn(worker_id):
    np.random.seed(np.random.get_state()[1][0] + worker_id)

if FLAGS.dataset == 'sunrgbd':
    sys.path.append(os.path.join(ROOT_DIR, 'sunrgbd'))
    from sunrgbd_detection_dataset_hd import SunrgbdDetectionVotesDataset, MAX_NUM_OBJ
    from model_util_sunrgbd import SunrgbdDatasetConfig
    DATASET_CONFIG = SunrgbdDatasetConfig()
    TEST_DATASET = SunrgbdDetectionVotesDataset(FLAGS.data_path, 'val', num_points=NUM_POINT,
        augment=False, use_color=FLAGS.use_color, use_height=(not FLAGS.no_height),
        use_v1=(not FLAGS.use_sunrgbd_v2))
elif FLAGS.dataset == 'scannet':
    sys.path.append(os.path.join(ROOT_DIR, 'scannet'))
    from scannet_detection_dataset_hd import ScannetDetectionDataset, MAX_NUM_OBJ
    from model_util_scannet import ScannetDatasetConfig
    DATASET_CONFIG = ScannetDatasetConfig()
    TEST_DATASET = ScannetDetectionDataset(FLAGS.data_path, 'val', num_points=NUM_POINT,
                                           augment=False, use_angle=False,
                                           use_color=FLAGS.use_color, use_height=(not FLAGS.no_height))
else:
    print('Unknown dataset %s. Exiting...'%(FLAGS.dataset))
    exit(-1)
print(len(TEST_DATASET))
TEST_DATALOADER = DataLoader(TEST_DATASET, batch_size=BATCH_SIZE,
    shuffle=FLAGS.shuffle_dataset, num_workers=4, worker_init_fn=my_worker_init_fn)

# Init the model and optimzier
MODEL = importlib.import_module(FLAGS.model) # import network module
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
num_input_channel = int(FLAGS.use_color)*3 + int(not FLAGS.no_height)*1

Detector = MODEL.HDNet

net = Detector(num_class=DATASET_CONFIG.num_class,
               num_heading_bin=DATASET_CONFIG.num_heading_bin,
               num_size_cluster=DATASET_CONFIG.num_size_cluster,
               mean_size_arr=DATASET_CONFIG.mean_size_arr,
               num_proposal=FLAGS.num_target,
               input_feature_dim=num_input_channel,
               vote_factor=FLAGS.vote_factor,
               sampling=FLAGS.cluster_sampling)

if torch.cuda.device_count() > 1:
    log_string("Let's use %d GPUs!" % (torch.cuda.device_count()))
    # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
    net = nn.DataParallel(net) 
net.to(device)
criterion = MODEL.get_loss

# Load the Adam optimizer
optimizer = optim.Adam(net.parameters(), lr=0.001)

# Load checkpoint if there is any
if CHECKPOINT_PATH is not None and os.path.isfile(CHECKPOINT_PATH):
    checkpoint = torch.load(CHECKPOINT_PATH)
    checkpoint_multigpu = dict()
    if torch.cuda.device_count() > 1:
        for name, param in checkpoint['model_state_dict'].items():
            checkpoint_multigpu.update({'module.' + name: param})
        net.load_state_dict(checkpoint_multigpu)
    else:
        net.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    log_string("Loaded checkpoint %s (epoch: %d)"%(CHECKPOINT_PATH, epoch))

# Used for AP calculation
CONFIG_DICT = {'remove_empty_box':False, 'use_3d_nms':True,
    'nms_iou':0.25, 'use_old_type_nms':False, 'cls_nms':True,
    'per_class_proposal': True, 'conf_thresh':0.05,
    'dataset_config':DATASET_CONFIG}

CONFIG_DICT_L = {'remove_empty_box':False, 'use_3d_nms':True,
    'nms_iou':0.5, 'use_old_type_nms':False, 'cls_nms':True,
    'per_class_proposal': True, 'conf_thresh':0.05,
    'dataset_config':DATASET_CONFIG}

# ------------------------------------------------------------------------- GLOBAL CONFIG END

def evaluate_one_epoch():
    stat_dict = {}

    ap_calculator = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh,
        class2type_map=DATASET_CONFIG.class2type)
    ap_calculator_l = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh*2,
        class2type_map=DATASET_CONFIG.class2type)

    net.eval() # set model to eval mode (for bn and dp)
    for batch_idx, batch_data_label in enumerate(TEST_DATALOADER):
        end_points = {}
        if batch_idx % 10 == 0:
            print('Eval batch: %d'%(batch_idx))
        for key in batch_data_label:
            batch_data_label[key] = batch_data_label[key].to(device)
        
        # Forward pass
        inputs = {'point_clouds': batch_data_label['point_clouds']}
        with torch.no_grad():
            end_points = net(inputs, end_points)

        # Compute loss
        for key in batch_data_label:
            end_points[key] = batch_data_label[key]
        loss, end_points = criterion(inputs, end_points, DATASET_CONFIG)

        # Accumulate statistics and print out
        for key in end_points:
            if 'loss' in key or 'acc' in key or 'ratio' in key:
                if key not in stat_dict: stat_dict[key] = 0
                stat_dict[key] += end_points[key].item()

        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT, opt_ang=(FLAGS.dataset == 'sunrgbd'))
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT) 
        ap_calculator.step(batch_pred_map_cls, batch_gt_map_cls)

        batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT_L, opt_ang=(FLAGS.dataset == 'sunrgbd'))
        batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT_L) 
        ap_calculator_l.step(batch_pred_map_cls, batch_gt_map_cls)
        
        if FLAGS.dump_results:
            dump_results(end_points, DUMP_DIR+'/result/', DATASET_CONFIG, TEST_DATASET, opt_ang=(FLAGS.dataset == 'sunrgbd'))


    # Log statistics
    for key in sorted(stat_dict.keys()):
        log_string('eval mean %s: %f'%(key, stat_dict[key]/(float(batch_idx+1))))

    metrics_dict = ap_calculator.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.25, eval %s: %f'%(key, metrics_dict[key]))
    metrics_dict = ap_calculator_l.compute_metrics()
    for key in metrics_dict:
        log_string('iou = 0.5, eval %s: %f'%(key, metrics_dict[key]))

    mean_loss = stat_dict['loss']/float(batch_idx+1)
    return mean_loss


def eval():
    log_string(str(datetime.now()))
    # Reset numpy seed.
    # REF: https://github.com/pytorch/pytorch/issues/5059
    np.random.seed()
    loss = evaluate_one_epoch()

if __name__=='__main__':
    eval()
