Skip to content

Instantly share code, notes, and snippets.

@bramton
Last active October 8, 2021 19:48
Show Gist options
  • Save bramton/1c2e67b7722594cf04450fb815441df0 to your computer and use it in GitHub Desktop.
Save bramton/1c2e67b7722594cf04450fb815441df0 to your computer and use it in GitHub Desktop.
Point Transformer metrics
import pickle
import numpy as np
nClasses = 13
with open("preds.pkl", "rb") as f:
true, pred = pickle.load(f)
# Overall accuracy
oa = np.sum(np.count_nonzero(true == pred))/true.size
print("Overall pointwise accuracy (OA): {:.3f}".format(oa))
# Per class acc/IoU
pc_iou = np.zeros((nClasses,1))
pc_acc = np.zeros((nClasses,1))
for j in np.arange(nClasses):
# Classwise IoU
num = np.bitwise_and(true==j, pred==j)
num = np.sum(num[:])
den = np.bitwise_or(true==j, pred==j)
den = np.sum(den[:])
iou = num/den
pc_iou[j] = iou
# Classwise accuracy
pc_acc[j] = num/np.count_nonzero(true==j)
print("Mean classwise accuracy (mAcc): {:.3f}".format(np.mean(pc_acc)))
print("Mean classwise IoU (mIoU): {:.3f}".format(np.mean(pc_iou)))
import pickle
import numpy as np
import torch
from point_transformer.models.point_transformer_seg import PointTransformerSemSegmentation as ptSemSeg
torch.cuda.empty_cache()
cuda = torch.device('cuda')
nClasses = 13
nPoints = 4096
bs = 16 # Batch size
ckpt = "outputs/semseg-point_transformer/epoch=23-val_loss=0.53-val_acc=0.883.ckpt"
model = ptSemSeg.load_from_checkpoint(checkpoint_path=ckpt)
model.prepare_data()
model.to(cuda)
model.eval()
dset = model.val_dset
true = np.zeros((len(dset), nPoints))
pred = np.zeros(true.shape)
dloader = torch.utils.data.DataLoader(dset, batch_size=bs, shuffle=True)
# Note, there is still some memory issue here. During training it uses a lot
# less memory. If anybody knows the reason, please let me know.
for k,data in enumerate(dloader):
print(k)
x, labels = data
true[k*bs:k*bs + labels.shape[0],:] = labels.squeeze()
x_data = x.to(cuda)
with torch.no_grad():
y = model.forward(x_data) # [batch x class x npoints]
y = y.squeeze().cpu().detach().numpy() # [batch x npoints]
pred[k*bs:k*bs+y.shape[0],:] = np.argmax(y, axis=1)
with open('preds.pkl', 'wb') as f:
pickle.dump([true, pred], f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment