-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_model_comparison.py
More file actions
225 lines (170 loc) · 6.63 KB
/
Copy pathrun_model_comparison.py
File metadata and controls
225 lines (170 loc) · 6.63 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
"""
Complete test script to compare all ground detection models.
Available models:
1. Original RANSAC - Geometric plane fitting
2. MiDaS - Depth estimation based ground detection
3. SegFormer - Transformer-based semantic segmentation
4. Detectron2 - Facebook's panoptic segmentation
5. SAM - Segment Anything Model v1
6. SAM2 - Segment Anything Model v2 (latest)
Usage:
python run_model_comparison.py
"""
import numpy as np
import cv2
import sys
import os
# Add path for imports
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from bridge_local_planner.gtrack_mapper_with_models import GTrackMapper
def generate_test_data():
"""Generate synthetic test data (point cloud + RGB + depth)."""
# Generate point cloud
n_points = 10000
# Floor points (80%)
n_floor = int(n_points * 0.8)
floor_x = np.random.uniform(-5, 5, n_floor)
floor_y = np.random.uniform(-5, 5, n_floor)
floor_z = np.random.normal(0, 0.05, n_floor)
floor_pts = np.stack([floor_x, floor_y, floor_z], axis=1)
# Object points (20%)
n_objects = n_points - n_floor
obj_x = np.random.uniform(-2, 2, n_objects)
obj_y = np.random.uniform(-2, 2, n_objects)
obj_z = np.random.uniform(0.5, 2.0, n_objects)
obj_pts = np.stack([obj_x, obj_y, obj_z], axis=1)
points = np.vstack([floor_pts, obj_pts])
# Generate RGB image
height, width = 480, 640
rgb_image = np.zeros((height, width, 3), dtype=np.uint8)
# Sky (top half)
rgb_image[:height//2, :] = [135, 206, 235]
# Floor (bottom half)
rgb_image[height//2:, :] = [128, 128, 128]
# Add objects
for _ in range(5):
x = np.random.randint(50, width-50)
y = np.random.randint(height//3, 2*height//3)
w = np.random.randint(20, 60)
h = np.random.randint(30, 80)
color = np.random.randint(0, 255, 3).tolist()
cv2.rectangle(rgb_image, (x, y), (x+w, y+h), color, -1)
# Generate depth image
depth_image = np.ones((height, width), dtype=np.float32) * 5.0
depth_image[height//2:, :] = 1.0 # Floor is closer
return points, rgb_image, depth_image
def main():
"""Main function to run model comparison."""
print("="*70)
print("GROUND DETECTION MODEL COMPARISON")
print("="*70)
# Available models with descriptions
models = {
'original': 'Original RANSAC (geometric plane fitting)',
'midas': 'MiDaS (depth estimation → ground detection)',
'segformer': 'SegFormer (transformer-based segmentation)',
'detectron2': 'Detectron2 (panoptic segmentation)',
'sam': 'SAM v1 (Segment Anything Model)',
'sam2': 'SAM v2 (latest Segment Anything Model)'
}
print("\nAvailable models:")
for i, (key, desc) in enumerate(models.items(), 1):
print(f" {i}. {key:12s} - {desc}")
# Initialize mapper
print("\n" + "-"*70)
print("Initializing GTrackMapper...")
mapper = GTrackMapper()
# Generate test data
print("Generating synthetic test data...")
points, rgb_image, depth_image = generate_test_data()
# Set images for model-based detection
mapper.set_rgb_image(rgb_image)
mapper.set_depth_image(depth_image)
# Test each model
print("\n" + "-"*70)
print("Testing models (this may take a while on first run)...")
print("-"*70)
# Test original RANSAC only
print(f"\n1. Testing ORIGINAL RANSAC only:")
mapper.set_detection_method(use_model=False)
success = mapper.apply_pointcloud(points)
if success:
print(" ✓ Original RANSAC completed")
else:
print(" ✗ Original RANSAC failed")
# Test each semantic segmentation model
test_models = ['midas', 'segformer', 'detectron2'] # SAM models require weights
for i, model_name in enumerate(test_models, 2):
print(f"\n{i}. Testing {model_name.upper()}:")
print(f" Description: {models[model_name]}")
try:
# Enable model detection
mapper.set_detection_method(use_model=True, model_name=model_name)
# Apply point cloud (runs both original and model for comparison)
success = mapper.apply_pointcloud(points)
if success:
print(f" ✓ {model_name} completed")
else:
print(f" ✗ {model_name} failed")
except Exception as e:
print(f" ✗ Error with {model_name}: {e}")
# Print timing summary
print("\n" + "="*70)
mapper.print_timing_summary()
# Instructions for SAM models
print("\n" + "-"*70)
print("NOTE: SAM and SAM2 require model weights to be downloaded:")
print(" - SAM: wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth")
print(" - SAM2: Download from the official SAM2 repository")
print("-"*70)
def example_usage():
"""Show example usage in code."""
print("\n" + "="*70)
print("EXAMPLE CODE USAGE")
print("="*70)
print("""
from bridge_local_planner.gtrack_mapper_with_models import GTrackMapper
import cv2
import numpy as np
# Initialize mapper
mapper = GTrackMapper()
# Load your data
points = np.load('your_pointcloud.npy') # Your 3D points
rgb_image = cv2.imread('your_image.jpg') # Your RGB image
# Set images for model detection
mapper.set_rgb_image(rgb_image)
mapper.set_depth_image(depth_image) # Optional for RGB-D models
# Method 1: Use original RANSAC
mapper.set_detection_method(use_model=False)
mapper.apply_pointcloud(points)
# Method 2: Use MiDaS model
mapper.set_detection_method(use_model=True, model_name='midas')
mapper.apply_pointcloud(points)
# Method 3: Use SegFormer model
mapper.set_detection_method(use_model=True, model_name='segformer')
mapper.apply_pointcloud(points)
# Method 4: Use Detectron2 model
mapper.set_detection_method(use_model=True, model_name='detectron2')
mapper.apply_pointcloud(points)
# Method 5: Use SAM (requires weights)
mapper.set_detection_method(use_model=True, model_name='sam')
mapper.apply_pointcloud(points)
# Method 6: Use SAM2 (requires weights)
mapper.set_detection_method(use_model=True, model_name='sam2')
mapper.apply_pointcloud(points)
# View timing comparison
mapper.print_timing_summary()
# Access the map data
obstacles = mapper.map_data['obstacles']
elevation = mapper.map_data['elevation']
histogram = mapper.map_data['histogram']
# Visualize
mapper.imshow_map_pyplot(obstacles, 'Obstacle Map')
""")
if __name__ == "__main__":
# Run main comparison
main()
# Show example usage
example_usage()
print("\n✅ Model comparison complete!")