v0.1.17; update 7-9
This commit is contained in:
@@ -80,7 +80,6 @@ def parse_args():
|
||||
|
||||
# Multiprocessing parameters, with different seeds
|
||||
{"name": "--multi", "action": "store_true", "default": False, "help": "Enable multiprocessing."},
|
||||
{"name": "--num-processes", "type": int, "default": 2, "help": "Number of parallel processes."},
|
||||
{"name": "--seeds", "type": int, "nargs": "+", "default": [0, 1, 2, 3, 4], "help": "List of random seeds for multiple runs."},
|
||||
{"name": "--base-masses", "type": float, "nargs": "+", "default": [0], "help": "List of base masses for the model."},
|
||||
{"name": "--frictions", "type": float, "nargs": "+", "default": [0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5], "help": "List of friction coefficients for the model."},
|
||||
@@ -91,8 +90,9 @@ def parse_args():
|
||||
# Stress pipeline parameters
|
||||
{"name": "--stress-benchmark", "action": "store_true", "default": False, "help": "Use stress pipeline to benchmark model robustness."},
|
||||
{"name": "--stress-terrain-names", "type": str, "nargs": "+", "default": ["flat", "slope", "wave", "stairs_up", "stairs_down"], "help": "List of terrain names for stress benchmark."},
|
||||
{"name": "--stress-num-processes", "type": int, "default": 2, "help": "Number of parallel processes for stress benchmark."},
|
||||
|
||||
# Common parameters
|
||||
{"name": "--num-processes", "type": int, "default": 2, "help": "Number of parallel processes for Multi or Stress benchmark."},
|
||||
{"name": "--compress-logs", "action": "store_true", "default": False, "help": "Compress and delete logs after run."},
|
||||
]
|
||||
for param in parameters:
|
||||
|
||||
@@ -2,16 +2,17 @@
|
||||
'''
|
||||
@File : progress_monitor.py
|
||||
@Time : 2025/12/27 00:10:07
|
||||
@Author : wty-yy
|
||||
@Author : wty-yy (with Gemini 3)
|
||||
@Version : 1.0
|
||||
@Blog : https://wty-yy.github.io/
|
||||
@Desc : Centralized progress monitoring for multiprocessing tasks using tqdm
|
||||
@Desc : Centralized progress monitoring with Dynamic Slot Management
|
||||
'''
|
||||
from tqdm import tqdm
|
||||
import multiprocessing
|
||||
from typing import Dict
|
||||
from threading import Thread
|
||||
from dataclasses import dataclass
|
||||
import heapq
|
||||
|
||||
class ProgressTypes:
|
||||
INIT = 'init' # Init progress (set total, desc)
|
||||
@@ -50,76 +51,111 @@ def report_progress(progress_data: ProgressData, msg_type, value=None, desc=None
|
||||
class ProgressMonitor:
|
||||
def __init__(self, total_rows):
|
||||
self.total_rows = total_rows
|
||||
self.bars: Dict[int, tqdm] = {}
|
||||
self.active_bars: Dict[int, Dict] = {}
|
||||
self.free_slots = []
|
||||
self.max_slot_used = 0
|
||||
|
||||
def _get_free_slot(self):
|
||||
""" Get a free screen display position """
|
||||
if self.free_slots:
|
||||
return heapq.heappop(self.free_slots)
|
||||
else:
|
||||
self.max_slot_used += 1
|
||||
return self.max_slot_used
|
||||
|
||||
def _release_slot(self, slot):
|
||||
""" Release a screen display position """
|
||||
heapq.heappush(self.free_slots, slot)
|
||||
|
||||
def listener_loop(self, queue):
|
||||
"""
|
||||
Run in a separate thread in the main process to consume the Queue and update tqdm.
|
||||
Listener thread in the main process
|
||||
"""
|
||||
# print(f"Monitor started for {self.total_rows} tasks...")
|
||||
for i in range(self.total_rows):
|
||||
self.bars[i] = tqdm(
|
||||
total=100,
|
||||
position=i,
|
||||
desc=f"Task {i} Pending...",
|
||||
bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}]",
|
||||
leave=True
|
||||
)
|
||||
# Create the main progress bar (Position 0)
|
||||
main_bar = tqdm(
|
||||
total=self.total_rows,
|
||||
position=0,
|
||||
desc="🚀 Total Progress",
|
||||
bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [Done: {n_fmt}] [{elapsed}<{remaining}]",
|
||||
leave=True,
|
||||
smoothing=0.1 # Optional: set smoothing factor (0-1) to prevent drastic ETA fluctuations due to slow tasks
|
||||
)
|
||||
|
||||
active_tasks = self.total_rows
|
||||
# Number of tasks still pending
|
||||
pending_tasks = self.total_rows
|
||||
|
||||
while active_tasks > 0:
|
||||
while pending_tasks > 0:
|
||||
record = queue.get()
|
||||
if record is None: # Poison pill signal
|
||||
break
|
||||
if record is None: break
|
||||
|
||||
task_id, msg_type, data = record
|
||||
if task_id not in self.bars:
|
||||
continue
|
||||
|
||||
bar = self.bars[task_id]
|
||||
|
||||
if msg_type == ProgressTypes.INIT:
|
||||
total = data['total']
|
||||
desc = data['desc']
|
||||
bar.reset(total=total)
|
||||
bar.set_description(desc)
|
||||
bar.refresh()
|
||||
|
||||
elif msg_type == ProgressTypes.UPDATE:
|
||||
val = data['value']
|
||||
bar.update(val)
|
||||
|
||||
elif msg_type == ProgressTypes.DESC:
|
||||
desc = data['desc']
|
||||
if desc:
|
||||
# --- Dynamically create/get sub progress bars ---
|
||||
if task_id not in self.active_bars:
|
||||
# Only create bar when message received to avoid Pending screen flicker
|
||||
slot = self._get_free_slot()
|
||||
# leave=False is key: after task completion, clear the line for new task reuse
|
||||
bar = tqdm(
|
||||
total=100,
|
||||
position=slot,
|
||||
desc=f"Task {task_id} Starting...",
|
||||
bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]",
|
||||
leave=False
|
||||
)
|
||||
self.active_bars[task_id] = {'bar': bar, 'slot': slot}
|
||||
|
||||
bar_info = self.active_bars[task_id]
|
||||
bar = bar_info['bar']
|
||||
|
||||
# --- Handle messages ---
|
||||
try:
|
||||
if msg_type == ProgressTypes.INIT:
|
||||
total = data.get('total', 100)
|
||||
desc = data.get('desc', '')
|
||||
bar.reset(total=total)
|
||||
bar.set_description(desc)
|
||||
bar.refresh()
|
||||
|
||||
elif msg_type == ProgressTypes.RESET:
|
||||
# Scenario: Level search finished, starting MultiPipeline, reset progress bar
|
||||
total = data['total']
|
||||
desc = data['desc']
|
||||
bar.reset(total=total)
|
||||
bar.set_description(desc)
|
||||
bar.refresh()
|
||||
|
||||
elif msg_type == ProgressTypes.FINISH:
|
||||
desc = data['desc']
|
||||
if desc:
|
||||
elif msg_type == ProgressTypes.UPDATE:
|
||||
val = data.get('value', 1)
|
||||
bar.update(val)
|
||||
|
||||
elif msg_type == ProgressTypes.DESC:
|
||||
desc = data.get('desc')
|
||||
if desc: bar.set_description(desc)
|
||||
|
||||
elif msg_type == ProgressTypes.RESET:
|
||||
total = data.get('total', 100)
|
||||
desc = data.get('desc', '')
|
||||
bar.reset(total=total)
|
||||
bar.set_description(desc)
|
||||
bar.refresh()
|
||||
# Note: We do not close the bar here to keep it displayed until all tasks are finished and closed together.
|
||||
active_tasks -= 1
|
||||
|
||||
elif msg_type == ProgressTypes.ERROR:
|
||||
desc = data['desc']
|
||||
bar.set_description(desc)
|
||||
bar.refresh()
|
||||
active_tasks -= 1
|
||||
bar.refresh()
|
||||
|
||||
elif msg_type == ProgressTypes.FINISH or msg_type == ProgressTypes.ERROR:
|
||||
# Update sub progress bar status
|
||||
desc = data.get('desc')
|
||||
if desc: bar.set_description(desc)
|
||||
bar.refresh()
|
||||
|
||||
# Close sub progress bar and release slot
|
||||
bar.close() # 因为 leave=False,这行会从屏幕消失
|
||||
slot = bar_info['slot']
|
||||
self._release_slot(slot)
|
||||
del self.active_bars[task_id]
|
||||
|
||||
# Update the main progress bar
|
||||
main_bar.update(1)
|
||||
main_bar.refresh()
|
||||
|
||||
pending_tasks -= 1
|
||||
|
||||
# After all tasks are finished, close all bars
|
||||
for bar in self.bars.values():
|
||||
bar.close()
|
||||
except Exception as e:
|
||||
pass # Prevent printing errors from disrupting layout
|
||||
|
||||
# Close all remaining bars (theoretically should be empty except main_bar)
|
||||
main_bar.close()
|
||||
for info in self.active_bars.values():
|
||||
info['bar'].close()
|
||||
|
||||
def start_progress_monitor_thread(total_rows):
|
||||
"""
|
||||
|
||||
@@ -1,3 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
@File : plot_radar_and_bar.py
|
||||
@Time : 2025/12/27 22:52:24
|
||||
@Author : wty-yy (with Gemini 3)
|
||||
@Version : 1.0
|
||||
@Blog : https://wty-yy.github.io/
|
||||
@Desc : 可视化评测结果,生成雷达图和柱状图
|
||||
'''
|
||||
import matplotlib.pyplot as plt
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
@@ -48,9 +57,10 @@ def load_data(file_paths):
|
||||
model_name = os.path.basename(raw_path).replace('.pt', '')
|
||||
|
||||
values = []
|
||||
summary = content['summary']
|
||||
for k in metric_keys:
|
||||
if k in content:
|
||||
raw_val = content[k]['mean@50']
|
||||
if k in summary:
|
||||
raw_val = summary[k]['mean@25']
|
||||
values.append(parse_value_string(raw_val))
|
||||
else:
|
||||
values.append(0.0)
|
||||
168
robogauge/utils/visualize/plot_terrain_levels.py
Normal file
168
robogauge/utils/visualize/plot_terrain_levels.py
Normal file
@@ -0,0 +1,168 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
@File : plot_terrain_levels.py
|
||||
@Time : 2025/12/27 23:04:01
|
||||
@Author : wty-yy (with Gemini 3)
|
||||
@Version : 1.0
|
||||
@Blog : https://wty-yy.github.io/
|
||||
@Desc : None
|
||||
@Desc : 可视化不同地形下随着摩擦力变化的关卡通过等级 (Terrain Level vs Friction)
|
||||
'''
|
||||
import matplotlib.pyplot as plt
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
import yaml
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
|
||||
# --- 配置 Matplotlib 样式 ---
|
||||
config = {
|
||||
"font.family": 'serif',
|
||||
"figure.figsize": (18, 10),
|
||||
"font.size": 12,
|
||||
"mathtext.fontset": 'cm',
|
||||
'axes.unicode_minus': False
|
||||
}
|
||||
plt.rcParams.update(config)
|
||||
plt.rcParams['font.serif'] = ['Times New Roman', 'DejaVu Serif', 'serif']
|
||||
|
||||
def load_terrain_data(file_paths):
|
||||
"""
|
||||
加载数据并按模型、地形、摩擦力组织
|
||||
Returns:
|
||||
model_data: {model_name: {terrain_type: {friction: level}}}
|
||||
"""
|
||||
target_terrains = ['wave', 'slope', 'stairs_up', 'stairs_down', 'obstacle']
|
||||
model_data = {}
|
||||
|
||||
# 用于从键名中提取 friction 的正则 (例如 friction1.25)
|
||||
friction_pattern = re.compile(r'friction([\d\.]+)')
|
||||
|
||||
for path in file_paths:
|
||||
if not os.path.exists(path):
|
||||
print(f"[ERROR] File not found: {path}")
|
||||
continue
|
||||
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
content = yaml.safe_load(f)
|
||||
|
||||
# 获取模型名称
|
||||
raw_path = content.get('model_path', 'Unknown_Model')
|
||||
model_name = os.path.basename(raw_path).replace('.pt', '')
|
||||
|
||||
if model_name not in model_data:
|
||||
model_data[model_name] = {t: {} for t in target_terrains}
|
||||
|
||||
# 遍历YAML中的每个测试条目
|
||||
for key, value in content.items():
|
||||
if key in ['model_path', 'summary']:
|
||||
continue
|
||||
|
||||
# 1. 检查地形是否在目标列表中
|
||||
t_name = value.get('terrain_name')
|
||||
if t_name not in target_terrains:
|
||||
continue
|
||||
|
||||
# 2. 从 key 中提取摩擦力
|
||||
match = friction_pattern.search(key)
|
||||
if not match:
|
||||
continue
|
||||
friction = float(match.group(1))
|
||||
|
||||
# 3. 获取地形等级 (取该条件下该地形的最大值,防止重复)
|
||||
level = value.get('terrain_level', 0)
|
||||
|
||||
# 记录数据
|
||||
current_max = model_data[model_name][t_name].get(friction, -1)
|
||||
if level > current_max:
|
||||
model_data[model_name][t_name][friction] = level
|
||||
|
||||
return model_data, target_terrains
|
||||
|
||||
def plot_results(model_data, terrain_labels, output_file=None):
|
||||
"""绘制 2x3 的子图"""
|
||||
if not model_data:
|
||||
print("No data to plot.")
|
||||
return
|
||||
|
||||
# 设置 2行3列 的布局
|
||||
fig, axes = plt.subplots(2, 3, figsize=(18, 10), sharex=True, sharey=True)
|
||||
axes = axes.flatten()
|
||||
|
||||
# 自动获取颜色映射
|
||||
colors = plt.cm.get_cmap("tab10", len(model_data))
|
||||
|
||||
# 遍历前5个地形进行绘图
|
||||
for i, t_name in enumerate(terrain_labels):
|
||||
ax = axes[i]
|
||||
|
||||
for idx, (model_name, terrains) in enumerate(model_data.items()):
|
||||
data_points = terrains.get(t_name, {})
|
||||
if not data_points:
|
||||
continue
|
||||
|
||||
# 排序 (Friction, Level)
|
||||
sorted_points = sorted(data_points.items())
|
||||
xs, ys = zip(*sorted_points)
|
||||
|
||||
ax.plot(xs, ys, marker='o', linestyle='-', linewidth=2,
|
||||
label=model_name, color=colors(idx), alpha=0.8)
|
||||
|
||||
ax.set_title(t_name.replace('_', ' ').title(), fontsize=14)
|
||||
ax.grid(True, linestyle='--', alpha=0.6)
|
||||
|
||||
# --- 第6张图:绘制平均值 (Average) ---
|
||||
ax_avg = axes[5]
|
||||
for idx, (model_name, terrains) in enumerate(model_data.items()):
|
||||
# 聚合该模型下所有地形的数据用于计算平均值
|
||||
# 结构: friction -> [level_slope, level_wave, ...]
|
||||
friction_agg = {}
|
||||
for t_name in terrain_labels:
|
||||
for f, l in terrains[t_name].items():
|
||||
if f not in friction_agg:
|
||||
friction_agg[f] = []
|
||||
friction_agg[f].append(l)
|
||||
|
||||
if not friction_agg:
|
||||
continue
|
||||
|
||||
# 计算平均值并排序
|
||||
sorted_frictions = sorted(friction_agg.keys())
|
||||
avg_levels = [np.mean(friction_agg[f]) for f in sorted_frictions]
|
||||
|
||||
ax_avg.plot(sorted_frictions, avg_levels, marker='s', linestyle='-', linewidth=2,
|
||||
label=model_name, color=colors(idx), alpha=0.8)
|
||||
|
||||
ax_avg.set_title('Average Performance', fontsize=14)
|
||||
ax_avg.grid(True, linestyle='--', alpha=0.6)
|
||||
|
||||
# 仅在最后一张图显示图例,避免遮挡
|
||||
ax_avg.legend(loc='best', frameon=False, title="Models")
|
||||
|
||||
# 最下面一行设置 X 轴标签
|
||||
for i in [3, 4, 5]:
|
||||
axes[i].set_xlabel('Friction ($\mu$)')
|
||||
|
||||
# 最左边一列设置 Y 轴标签
|
||||
for i in [0, 3]:
|
||||
axes[i].set_ylabel('Terrain Level')
|
||||
|
||||
plt.tight_layout()
|
||||
# 稍微调整间距防止标签重叠
|
||||
# plt.subplots_adjust(wspace=0.1, hspace=0.15)
|
||||
|
||||
if output_file:
|
||||
plt.savefig(output_file, dpi=300, bbox_inches='tight')
|
||||
print(f"Plot saved to {output_file}")
|
||||
else:
|
||||
plt.show()
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Plot terrain level vs friction curves.")
|
||||
parser.add_argument('files', metavar='F', type=str, nargs='+', help='YAML result files')
|
||||
parser.add_argument('--out', type=str, default='terrain_analysis.jpg', help='Output image filename')
|
||||
args = parser.parse_args()
|
||||
|
||||
data, labels = load_terrain_data(args.files)
|
||||
plot_results(data, labels, output_file=args.out)
|
||||
Reference in New Issue
Block a user