use pointnet semantic segmentation pytorch implementation github repo and make sure that following code works. my input is x y z r g b. i get following error
2024-09-16 00:33:11,727 - INFO - Script arguments:
2024-09-16 00:33:11,727 - INFO - input_file: data/org_data/s3dis/office_3.txt
2024-09-16 00:33:11,727 - INFO - output_folder: data/segmented/s3dis/
2024-09-16 00:33:11,727 - INFO - batch_size: 32
2024-09-16 00:33:11,727 - INFO - num_point: 16384
2024-09-16 00:33:11,727 - INFO - model_path: log/sem_seg/pointnet2_sem_seg/checkpoints/best_model.pth
2024-09-16 00:33:11,727 - INFO - model_name: log.sem_seg.pointnet2_sem_seg.pointnet2_sem_seg
2024-09-16 00:33:11,727 - INFO - use_full_cloud: False
2024-09-16 00:33:11,727 - INFO - Using device: cpu
2024-09-16 00:33:11,728 - INFO - Loading model
2024-09-16 00:33:11,825 - INFO - Model loaded successfully
2024-09-16 00:33:11,825 - INFO - Reading point cloud from data/org_data/s3dis/office_3.txt
2024-09-16 00:33:12,317 - INFO - Starting segmentation
2024-09-16 00:33:24,781 - ERROR - Segmentation failed: Given groups=1, weight of size [32, 12, 1, 1], expected input[1, 16387, 32, 1024] to have 12 channels, but got 16387 channels instead
import torch
import numpy as np
import importlib
import os
import sys
import open3d as o3d
import argparse
import logging
from datetime import datetime
Add the directory containing your model to the Python path
LABEL_TO_NAMES = {i: name for i, name in enumerate(COLOR_MAP.keys())}
def parse_args():
parser = argparse.ArgumentParser(description='Point Cloud Segmentation')
parser.add_argument('--input_file', type=str, required=True, help='Path to the input point cloud file')
parser.add_argument('--output_folder', type=str, required=True, help='Path to the output folder')
parser.add_argument('--batch_size', type=int, default=32, help='Batch size for processing')
parser.add_argument('--num_point', type=int, default=16384, help='Number of points per sample')
parser.add_argument('--model_path', type=str, default='log/sem_seg/pointnet2_sem_seg/checkpoints/best_model.pth', help='Path to the trained model')
parser.add_argument('--model_name', type=str, default='log.sem_seg.pointnet2_sem_seg.pointnet2_sem_seg', help='Model script')
parser.add_argument('--use_full_cloud', action='store_true', help='Use full point cloud for segmentation')
return parser.parse_args()
def read_point_cloud(file_path):
"""
Read XYZ and RGB data from a text file.
"""
data = np.loadtxt(file_path, delimiter=' ')
xyz = data[:, :3]
if data.shape[1] >=6:
rgb = data[:, 3:6]
else:
rgb = None
return xyz, rgb
def sample_point_cloud(xyz, rgb, num_points):
"""
Randomly sample a subset of points from the point cloud.
"""
num_total = xyz.shape[0]
if num_total <= num_points:
if rgb is not None:
return xyz, rgb
return xyz, None
code
indices = np.random.choice(num_total, num_points, replace=False)
if rgb is not None:
return xyz[indices], rgb[indices]
return xyz[indices], None
def process_point_cloud(xyz, rgb, num_neighbors=30):
"""
Process XYZ and RGB data using Open3D.
Correct normalization and feature extraction.
"""
# Create Open3D PointCloud object
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
if rgb is not None:
pcd.colors = o3d.utility.Vector3dVector(rgb / 255.0) # Normalize RGB to [0,1]
code
# Normalize the point cloud: zero mean, unit scale
points = np.asarray(pcd.points)
mean = np.mean(points, axis=0)
points -= mean
scale = np.max(np.linalg.norm(points, axis=1))
points /= scale
pcd.points = o3d.utility.Vector3dVector(points)
# Estimate normals
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamKNN(knn=num_neighbors))
pcd.orient_normals_consistent_tangent_plane(k=num_neighbors)
normals = np.asarray(pcd.normals)
# Combine XYZ and normals (6 channels)
points = np.hstack((points, normals))
# If using RGB, include it
if rgb is not None:
points = np.hstack((points, np.asarray(pcd.colors)))
return points
def segment_point_cloud(classifier, xyz, rgb, device, use_full_cloud=True, num_point=NUM_POINT):
"""
Perform segmentation on input XYZ and RGB data.
"""
if not use_full_cloud:
xyz, rgb = sample_point_cloud(xyz, rgb, num_point)
code
points = process_point_cloud(xyz, rgb)
# Move to device
points_tensor = torch.from_numpy(points).float().to(device)
# Depending on the model's expected input dimensions
# The model likely expects [B, C, N]
points_tensor = points_tensor.unsqueeze(0) # [1, C, N]
# Check if num of points matches, else pad or truncate
current_num_points = points_tensor.shape[2]
if current_num_points < num_point:
pad_size = num_point - current_num_points
padding = torch.zeros((1, points_tensor.shape[1], pad_size)).to(device)
points_tensor = torch.cat([points_tensor, padding], dim=2)
elif current_num_points > num_point:
points_tensor = points_tensor[:, :, :num_point]
# Perform segmentation
with torch.no_grad():
seg_pred, _ = classifier(points_tensor)
# Get predicted labels
batch_pred_label = seg_pred.contiguous().data.max(2)[1].cpu().numpy()
pred_labels = batch_pred_label[0][:xyz.shape[0]] # Get the labels for actual points
return pred_labels, points
def save_as_txt(original_xyz, labels, output_file):
"""
Save the point cloud with segmentation labels as a TXT file.
"""
with open(output_file, 'w') as f:
for i in range(original_xyz.shape[0]):
xyz = original_xyz[i]
label = labels[i]
label_name = LABEL_TO_NAMES.get(label, 'unknown')
label_color = COLOR_MAP.get(label_name, [0, 0, 0])
f.write(f"{xyz[0]:.6f} {xyz[1]:.6f} {xyz[2]:.6f} {label_color[0]} {label_color[1]} {label_color[2]}\n")
def main(args):
# Set up logging
os.makedirs(args.output_folder, exist_ok=True)
log_file = os.path.join(args.output_folder, f"segmentation_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt")
logger = setup_logger(log_file)
code
# Log arguments
logger.info("Script arguments:")
for arg, value in vars(args).items():
logger.info(f"{arg}: {value}")
# Set output file path
output_file_txt = os.path.join(args.output_folder, os.path.basename(args.input_file))
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logger.info(f"Using device: {device}")
# Load your model
logger.info("Loading model")
MODEL = importlib.import_module(args.model_name)
classifier = MODEL.get_model(NUM_CLASSES)
classifier.to(device)
try:
# Load model weights
state_dict = torch.load(args.model_path, map_location=device)['model_state_dict']
# Remove 'module.' prefix if present
new_state_dict = {}
for k, v in state_dict.items():
if k.startswith('module.'):
new_state_dict[k[7:]] = v
else:
new_state_dict[k] = v
classifier.load_state_dict(new_state_dict)
classifier.eval()
logger.info("Model loaded successfully")
except Exception as e:
logger.error(f"Failed to load model: {str(e)}")
return
# Read point cloud data
logger.info(f"Reading point cloud from {args.input_file}")
try:
xyz, rgb = read_point_cloud(args.input_file)
except Exception as e:
logger.error(f"Failed to read point cloud: {str(e)}")
return
# Perform segmentation
logger.info("Starting segmentation")
try:
pred_labels, processed_points = segment_point_cloud(classifier, xyz, rgb, device, args.use_full_cloud, args.num_point)
except Exception as e:
logger.error(f"Segmentation failed: {str(e)}")
return
# Save results as TXT file
logger.info(f"Saving results to {output_file_txt}")
try:
save_as_txt(xyz, pred_labels, output_file_txt)
except Exception as e:
logger.error(f"Failed to save segmented point cloud: {str(e)}")
return
# Print and log predicted labels
label_counts = {}
for label in pred_labels:
label_name = LABEL_TO_NAMES.get(label, 'unknown')
if label_name not in label_counts:
label_counts[label_name] = 0
label_counts[label_name] += 1
logger.info("Predicted label counts:")
for label_name, count in label_counts.items():
logger.info(f"{label_name}: {count}")
logger.info(f"Segmented {len(pred_labels)} points. Results saved to {output_file_txt}")
if name == 'main':
args = parse_args()
main(args)
The error you're encountering stems from a mismatch between the expected input dimensions of the PointNet model and the actual dimensions of the input tensor you're providing. Specifically, the model expects an input tensor with a certain number of channels (12 in this case), but it's receiving a tensor with a significantly larger number of channels (16387). This discrepancy is due to an incorrect arrangement of the tensor dimensions before passing it to the model.
Let's break down the issue and provide a step-by-step solution to ensure your code works correctly.
Understanding the Error
Error Message:
code
Segmentation failed: Given groups=1, weight of size [32, 12, 1, 1], expected input[1, 16387, 32, 1024] to have 12 channels, but got 16387 channels instead
Received Input:[1, 16387, 32, 1024] (i.e., 16387 channels)
Root Cause:
The model expects the input tensor to have the shape [Batch, Channels, Num_Points] (e.g., [1, 12, 16384]). However, due to incorrect tensor manipulation, the input tensor is being shaped incorrectly as [1, Num_Points, Channels, ...], leading the model to interpret the number of points as the channel dimension.
Solution
1. Correct the Tensor Shape
Ensure that the input tensor to the model has the shape [Batch, Channels, Num_Points]. In your current code, after processing the point cloud, the tensor is likely shaped as [Batch, Num_Points, Channels], which is incorrect.
Modify the segment_point_cloud Function:
Replace the existing tensor manipulation with the following steps to correctly arrange the tensor dimensions.
python
def segment_point_cloud(classifier, xyz, rgb, device, use_full_cloud=True, num_point=NUM_POINT):
"""
Perform segmentation on input XYZ and RGB data.
"""
if not use_full_cloud:
xyz, rgb = sample_point_cloud(xyz, rgb, num_point)
points = process_point_cloud(xyz, rgb)
# Move to device
points_tensor = torch.from_numpy(points).float().to(device)
# Transpose to [Channels, Num_Points]
points_tensor = points_tensor.transpose(0, 1)
# Add batch dimension: [Batch, Channels, Num_Points]
points_tensor = points_tensor.unsqueeze(0)
# Check if num of points matches, else pad or truncate
current_num_points = points_tensor.shape[2]
if current_num_points < num_point:
pad_size = num_point - current_num_points
padding = torch.zeros((1, points_tensor.shape[1], pad_size)).to(device)
points_tensor = torch.cat([points_tensor, padding], dim=2)
elif current_num_points > num_point:
points_tensor = points_tensor[:, :, :num_point]
# Perform segmentation
with torch.no_grad():
seg_pred, _ = classifier(points_tensor)
# Get predicted labels
batch_pred_label = seg_pred.contiguous().data.max(2)[1].cpu().numpy()
pred_labels = batch_pred_label[0][:xyz.shape[0]] # Get the labels for actual points
return pred_labels, points
Explanation:
Transpose the Tensor:
points_tensor = points_tensor.transpose(0, 1) changes the shape from [Num_Points, Channels] to [Channels, Num_Points].
Add Batch Dimension:
points_tensor = points_tensor.unsqueeze(0) changes the shape to [Batch, Channels, Num_Points].
2. Ensure the Number of Channels Matches
The model expects 12 channels, but based on your input (x, y, z, r, g, b), you currently have 6 channels. Additionally, you might be computing normals, which adds 3 more channels, totaling 9 channels.
To resolve this, you need to ensure that the input tensor has exactly 12 channels. Here's how you can do it:
Modify the process_point_cloud Function:
python
def process_point_cloud(xyz, rgb, num_neighbors=30):
"""
Process XYZ and RGB data using Open3D.
Correct normalization and feature extraction.
"""
# Create Open3D PointCloud object
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
if rgb is not None:
pcd.colors = o3d.utility.Vector3dVector(rgb / 255.0) # Normalize RGB to [0,1]
# Normalize the point cloud: zero mean, unit scale
points = np.asarray(pcd.points)
mean = np.mean(points, axis=0)
points -= mean
scale = np.max(np.linalg.norm(points, axis=1))
points /= scale
pcd.points = o3d.utility.Vector3dVector(points)
# Estimate normals
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamKNN(knn=num_neighbors))
pcd.orient_normals_consistent_tangent_plane(k=num_neighbors)
normals = np.asarray(pcd.normals)
# Combine XYZ and normals (6 channels)
points = np.hstack((points, normals))
# If using RGB, include it (additional 3 channels)
if rgb is not None:
points = np.hstack((points, np.asarray(pcd.colors)))
# Verify number of channels
current_channels = points.shape[1]
required_channels = 12
if current_channels < required_channels:
# Pad with zeros to reach required channels
padding = np.zeros((points.shape[0], required_channels - current_channels))
points = np.hstack((points, padding))
elif current_channels > required_channels:
# Truncate to required channels
points = points[:, :required_channels]
return points
Explanation:
Compute Current Channels:
After combining XYZ, normals, and RGB, calculate the current number of channels.
Pad or Truncate Channels:
Pad: If the current number of channels is less than 12, pad the remaining channels with zeros.
Truncate: If there are more than 12 channels, truncate the excess channels.
Note: Padding with zeros is a simple approach, but ideally, you should understand what the additional channels represent and provide meaningful data. If the model was trained with specific features in these additional channels, you might need to compute or source these features accordingly.
3. Verify Model Compatibility
Ensure that the model you're loading (pointnet2_sem_seg) is compatible with the input data format you're providing. The model should be expecting an input with 12 channels as per the error message. If the model was originally trained with a different number of channels, you might need to adjust either the model architecture or the input data accordingly.
4. Complete Updated Code Snippet
Here's the updated version of the critical parts of your script incorporating the fixes:
python
def process_point_cloud(xyz, rgb, num_neighbors=30):
"""
Process XYZ and RGB data using Open3D.
Correct normalization and feature extraction.
"""
# Create Open3D PointCloud object
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
if rgb is not None:
pcd.colors = o3d.utility.Vector3dVector(rgb / 255.0) # Normalize RGB to [0,1]
# Normalize the point cloud: zero mean, unit scale
points = np.asarray(pcd.points)
mean = np.mean(points, axis=0)
points -= mean
scale = np.max(np.linalg.norm(points, axis=1))
points /= scale
pcd.points = o3d.utility.Vector3dVector(points)
# Estimate normals
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamKNN(knn=num_neighbors))
pcd.orient_normals_consistent_tangent_plane(k=num_neighbors)
normals = np.asarray(pcd.normals)
# Combine XYZ and normals (6 channels)
points = np.hstack((points, normals))
# If using RGB, include it (additional 3 channels)
if rgb is not None:
points = np.hstack((points, np.asarray(pcd.colors)))
# Verify number of channels
current_channels = points.shape[1]
required_channels = 12
if current_channels < required_channels:
# Pad with zeros to reach required channels
padding = np.zeros((points.shape[0], required_channels - current_channels))
points = np.hstack((points, padding))
elif current_channels > required_channels:
# Truncate to required channels
points = points[:, :required_channels]
return points
def segment_point_cloud(classifier, xyz, rgb, device, use_full_cloud=True, num_point=NUM_POINT):
"""
Perform segmentation on input XYZ and RGB data.
"""
if not use_full_cloud:
xyz, rgb = sample_point_cloud(xyz, rgb, num_point)
points = process_point_cloud(xyz, rgb)
# Move to device
points_tensor = torch.from_numpy(points).float().to(device)
# Transpose to [Channels, Num_Points]
points_tensor = points_tensor.transpose(0, 1)
# Add batch dimension: [Batch, Channels, Num_Points]
points_tensor = points_tensor.unsqueeze(0)
# Check if num of points matches, else pad or truncate
current_num_points = points_tensor.shape[2]
if current_num_points < num_point:
pad_size = num_point - current_num_points
padding = torch.zeros((1, points_tensor.shape[1], pad_size)).to(device)
points_tensor = torch.cat([points_tensor, padding], dim=2)
elif current_num_points > num_point:
points_tensor = points_tensor[:, :, :num_point]
# Perform segmentation
with torch.no_grad():
seg_pred, _ = classifier(points_tensor)
# Get predicted labels
batch_pred_label = seg_pred.contiguous().data.max(2)[1].cpu().numpy()
pred_labels = batch_pred_label[0][:xyz.shape[0]] # Get the labels for actual points
return pred_labels, points
5. Additional Recommendations
Validate the Number of Channels:
Before running the segmentation, it's a good practice to print out the shape of points_tensor to ensure it matches [1, 12, 16384].
Model Training Consistency:
Ensure that the model was trained with the same number of input channels (12). If not, you might need to retrain the model with the correct number of input channels or adjust the current input data accordingly.
Feature Engineering:
If the model expects specific features in the additional channels (beyond XYZ, Normals, RGB), you should incorporate those features into your process_point_cloud function.
Device Compatibility:
While your logs indicate that you're using cpu, if you plan to switch to cuda, ensure that all tensors and the model are moved to the GPU appropriately.
Conclusion
By correctly arranging the tensor dimensions and ensuring the number of channels matches the model's expectations, you should be able to resolve the segmentation error. Make sure to adapt any additional features as required by the specific PointNet model variant you're using.
If you continue to face issues after these adjustments, please provide more details about the model architecture and how it was trained, so further assistance can be provided.