-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
92 lines (70 loc) · 2.96 KB
/
test.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from torch.utils.data import Dataset, DataLoader
from sklearn.preprocessing import LabelEncoder
import ast
from model import PointNet
from sklearn.metrics import accuracy_score, classification_report
class PointCloudDataset(Dataset):
def __init__(self, csv_file):
self.data = pd.read_csv(csv_file)
# Convert string representation of points to numpy arrays
self.points = self.data['point'].apply(lambda x: np.array(ast.literal_eval(x.replace('];[', '], ['))))
# Encode labels
self.label_encoder = LabelEncoder()
self.labels = self.label_encoder.fit_transform(self.data['label'])
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
points = torch.FloatTensor(self.points[idx])
label = torch.LongTensor([self.labels[idx]])[0]
return points, label
def test_pointnet(model, test_loader, device='cuda'):
model.eval()
model = model.to(device)
all_preds = []
all_labels = []
with torch.no_grad():
for points, labels in test_loader:
# Move data to device
points = points.to(device)
labels = labels.to(device)
points = points.permute(0, 2, 1) # (B, N, 3) -> (B, 3, N)
# Forward pass
outputs, _, _ = model(points)
# Predictions
_, predicted = outputs.max(1)
all_preds.append(predicted.cpu().numpy())
all_labels.append(labels.cpu().numpy())
# Concatenate all predictions and labels
all_preds = np.concatenate(all_preds, axis=0)
all_labels = np.concatenate(all_labels, axis=0)
return all_preds, all_labels
def main():
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {device}")
# Load testing dataset
print('Loading testing dataset...')
test_dataset = PointCloudDataset('data_generation/test_isaac_sim_3d.csv')
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
print('Testing dataset loaded!')
# Initialize model with the same number of classes
num_classes = len(test_dataset.label_encoder.classes_)
model = PointNet(num_classes=num_classes)
print('Model initialized!')
# Load saved model checkpoint
checkpoint_path = 'pointnet_model.pth'
model.load_state_dict(torch.load(checkpoint_path, map_location=device))
print(f"Checkpoint loaded from {checkpoint_path}")
# Test the model
print("Testing the model...")
predictions, labels = test_pointnet(model, test_loader, device)
# Compute accuracy and print classification report
accuracy = accuracy_score(labels, predictions)
print(f"Test Accuracy: {accuracy:.4f}")
print("\nClassification Report:")
print(classification_report(labels, predictions, target_names=test_dataset.label_encoder.classes_))
if __name__ == "__main__":
main()