forked from learning3d/assignment5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_seg.py
78 lines (59 loc) · 3.09 KB
/
eval_seg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import argparse
import torch
from models import seg_model
from data_loader import get_data_loader
from utils import create_dir, viz_seg
from tqdm import tqdm
import pdb
def create_parser():
"""Creates a parser for command-line arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--num_seg_class', type=int, default=6, help='The number of segmentation classes')
parser.add_argument('--num_points', type=int, default=10000, help='The number of points per object to be included in the input data')
# Directories and checkpoint/sample iterations
parser.add_argument('--load_checkpoint', type=str, default='model_epoch_0')
parser.add_argument('--i', type=int, default=0, help="index of the object to visualize")
parser.add_argument('--test_data', type=str, default='./data/seg/data_test.npy')
parser.add_argument('--test_label', type=str, default='./data/seg/label_test.npy')
parser.add_argument('--output_dir', type=str, default='./output')
parser.add_argument('--exp_name', type=str, default="exp", help='The name of the experiment')
return parser
if __name__ == '__main__':
parser = create_parser()
args = parser.parse_args()
args.device = torch.device("cuda" if torch.cuda.is_available() else 'cpu')
create_dir(args.output_dir)
# ------ TO DO: Initialize Model for Segmentation Task ------
model = seg_model().to(args.device)
# Load Model Checkpoint
model_path = './checkpoints/seg/{}.pt'.format(args.load_checkpoint)
with open(model_path, 'rb') as f:
state_dict = torch.load(f, map_location=args.device)
model.load_state_dict(state_dict)
model.eval()
print ("successfully loaded checkpoint from {}".format(model_path))
# Sample Points per Object
ind = np.random.choice(10000,args.num_points, replace=False)
test_data = torch.from_numpy((np.load(args.test_data))[:,ind,:])
test_label = torch.from_numpy((np.load(args.test_label))[:,ind])
# ------ TO DO: Make Prediction ------
batch_size = 32
num_batch = (test_data.shape[0] // batch_size)+1
pred_label = torch.zeros_like(test_label)
for i in tqdm(range(num_batch)):
# pdb.set_trace()
pred = model(test_data[i*batch_size: (i+1)*batch_size].to(args.device))
curr_pred_label = torch.argmax(pred, -1, keepdim=False).cpu()
# curr_pred_label = list(curr_pred_label)
# pred_label.extend(curr_pred_label)
pred_label[i*batch_size: (i+1)*batch_size, :] = curr_pred_label
pred_label = torch.Tensor(pred_label).cpu()
test_accuracy = pred_label.eq(test_label.data).cpu().sum().item() / (test_label.reshape((-1,1)).size()[0])
print ("test accuracy: {}".format(test_accuracy))
# Visualize Segmentation Result (Pred VS Ground Truth)
for idx in range(len(test_data)):
# pdb.set_trace()
viz_seg(test_data[idx], test_label[idx], "{}/seg/gt_{}_{}.gif".format(args.output_dir, args.exp_name, idx), args.device)
viz_seg(test_data[idx], pred_label[idx], "{}/seg/pred_{}_{}.gif".format(args.output_dir, args.exp_name, idx), args.device)