File size: 15,816 Bytes
766d56c |
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
import os
import torch
import numpy as np
from tqdm import tqdm
import torch.nn.functional as F
from utils.metric import *
from dataset.data_util import MinMaxScaler
from utils.utils import mask_data_general, ddp_setup, continuous_mask_data, continuous_time_based_mask, mask_multiple_segments
# Diffusion model will be imported directly in the main script that calls this test function.
# from diffProModel.Diffusion import Diffusion # No, pass model as argument
def test_model(test_dataloader, diffusion_model, short_samples_model, config, epoch,
prototypes, device, logger, exp_dir):
"""
Test the unified Diffusion model (DDPM or DDIM) on the test dataset.
Args:
test_dataloader: DataLoader for test data.
diffusion_model: The unified diffusion model (instance of diffProModel.Diffusion.Diffusion).
short_samples_model: Trajectory transformer model for feature extraction.
config: Configuration object.
epoch: Current epoch number (or identifier for the test run).
prototypes: Prototype vectors (e.g., from TrajectoryTransformer or K-Means).
device: Device to run the model on (already determined by the caller).
logger: Logger object.
exp_dir: Experiment directory path.
"""
# Determine distributed status and local_rank first
distributed = config.training.dis_gpu
local_rank = 0
if distributed:
# If DDP is active, LOCAL_RANK should be set by the environment.
# ddp_setup should have been called by the parent process (e.g., train_main or main for DDP launch)
# test_model itself typically does not re-initialize DDP.
try:
local_rank = int(os.environ.get('LOCAL_RANK', 0))
except ValueError:
if logger: logger.warning("LOCAL_RANK environment variable not a valid integer. Defaulting to 0.")
local_rank = 0
# The 'device' argument passed to this function should be the correct one to use.
thresholds = [i for i in range(1000, 11000, 1000)] # Thresholds for TC metric
# Initialize lists to store metrics for each batch
mtd_list, mppe_list, maepp_list, maeps_list, aptc_list, avg_aptc_list, max_td_list = [], [], [], [], [], [], []
# Get sampling parameters from config (assuming they are in config.sampling)
sampling_type = getattr(config.sampling, 'type', 'ddpm') # Default to ddpm if not specified
ddim_steps = getattr(config.sampling, 'ddim_steps', 50)
ddim_eta = getattr(config.sampling, 'ddim_eta', 0.0)
debug_mode = getattr(config, 'debug', False) # General debug flag
if logger and local_rank == 0: # Ensure logger operations happen on rank 0 if distributed
logger.info(f"Testing with sampling_type: {sampling_type} for epoch {epoch}")
if sampling_type == 'ddim':
logger.info(f"DDIM steps: {ddim_steps}, DDIM eta: {ddim_eta}")
diffusion_model.eval() # Ensure diffusion model is in eval mode
short_samples_model.eval() # Ensure feature extractor is in eval mode
pbar_desc = f"Epoch {epoch} Test Progress ({sampling_type.upper()})"
for batch_idx, (abs_time, lat, lng) in enumerate(tqdm(test_dataloader, desc=pbar_desc, disable=(local_rank != 0))):
if debug_mode and logger and local_rank == 0:
logger.info(f"Batch {batch_idx} - Input shapes: abs_time {abs_time.shape}, lat {lat.shape}, lng {lng.shape}")
logger.info(f"Input data stats - abs_time: min={abs_time.min().item():.4f}, max={abs_time.max().item():.4f}, " +
f"lat: min={lat.min().item():.4f}, max={lat.max().item():.4f}, " +
f"lng: min={lng.min().item():.4f}, max={lng.max().item():.4f}")
if torch.isnan(abs_time).any() or torch.isnan(lat).any() or torch.isnan(lng).any():
if logger and local_rank == 0: logger.error(f"Batch {batch_idx} - NaN detected in input data!")
continue
# Prepare input tensor (ground truth for start/end points and for scaling)
# This testx_raw is used for scaler fitting and as test_x0 for diffusion model
testx_raw = torch.stack([abs_time, lat, lng], dim=-1).to(device)
# Use global normalization parameters for consistency
scaler = MinMaxScaler(global_params_file='./data/robust_normalization_params.json')
scaler.fit(testx_raw) # This does nothing for global scaler, but maintains interface
testx_scaled = scaler.transform(testx_raw) # Scale data
if debug_mode and logger and local_rank == 0:
logger.info(f"Scaler min: {scaler.min_val.flatten().cpu().numpy()}, max: {scaler.max_val.flatten().cpu().numpy()}")
if torch.isnan(testx_scaled).any():
if logger and local_rank == 0:
logger.error(f"Batch {batch_idx} - NaN detected after scaling!")
if torch.any(scaler.max_val == scaler.min_val):
logger.error("Division by zero in scaler possible: max_val equals min_val for some features.")
continue
# Permute for diffusion model input: (batch_size, num_features, traj_length)
testx_scaled_permuted = testx_scaled.permute(0, 2, 1)
# Apply masking
if config.masking_strategy == 'general':
masked_condition_permuted = mask_data_general(testx_scaled_permuted)
elif config.masking_strategy == 'continuous':
masked_condition_permuted = continuous_mask_data(testx_scaled_permuted, config.mask_ratio)
elif config.masking_strategy == 'time_based':
masked_condition_permuted = continuous_time_based_mask(testx_scaled_permuted, points_to_mask=config.mask_points_per_hour)
elif config.masking_strategy == 'multi_segment':
masked_condition_permuted = mask_multiple_segments(testx_scaled_permuted, points_per_segment=config.mask_segments)
else:
raise ValueError(f"Unknown masking strategy: {config.masking_strategy}")
masked_condition = masked_condition_permuted.permute(0, 2, 1)
with torch.no_grad():
_, query_features = short_samples_model(masked_condition)
if torch.isnan(query_features).any():
if logger and local_rank == 0: logger.error(f"Batch {batch_idx} - NaN detected in query_features!")
continue
if torch.isnan(prototypes).any():
if logger and local_rank == 0: logger.error(f"Batch {batch_idx} - NaN detected in provided prototypes!")
continue
# Match query features with prototypes (e.g., via cosine similarity and softmax attention)
# This logic should align with how matched_prototypes are generated during training
cos_sim = F.cosine_similarity(query_features.unsqueeze(1), prototypes.unsqueeze(0), dim=-1)
if torch.isnan(cos_sim).any():
if logger and local_rank == 0: logger.error(f"Batch {batch_idx} - NaN detected in cos_sim!")
continue
# Using the same attention-weighted sum as in the unified training script
d_k = query_features.size(-1)
scaled_cos_sim = F.softmax(cos_sim / np.sqrt(d_k), dim=-1)
matched_prototypes_for_diffusion = torch.matmul(scaled_cos_sim, prototypes).to(device)
if torch.isnan(matched_prototypes_for_diffusion).any():
if logger and local_rank == 0: logger.error(f"Batch {batch_idx} - NaN detected in matched_prototypes!")
continue
if debug_mode and logger and local_rank == 0:
logger.info(f"Sampling with type: {sampling_type}, DDIM steps: {ddim_steps}, eta: {ddim_eta}")
logger.info(f"Input to diffusion model (testx_scaled_permuted) shape: {testx_scaled_permuted.shape}, "
f"masked condition (masked_condition_permuted) shape: {masked_condition_permuted.shape}, "
f"matched prototypes shape: {matched_prototypes_for_diffusion.shape}")
try:
pred_x0_scaled = diffusion_model.sample(
test_x0=testx_scaled_permuted, # Ground truth (scaled) for start/end points and reference
attr=masked_condition_permuted, # Masked data for conditional U-Net input (GuideNet attr)
prototype=matched_prototypes_for_diffusion, # Matched prototypes for GuideNet
sampling_type=sampling_type,
ddim_num_steps=ddim_steps,
ddim_eta=ddim_eta
)
if torch.isnan(pred_x0_scaled).any():
if logger and local_rank == 0: logger.error(f"Batch {batch_idx} - NaN detected in Diffusion model output!")
continue
except Exception as e:
if logger and local_rank == 0: logger.error(f"Exception during Diffusion model sampling: {str(e)}")
import traceback
if logger and local_rank == 0: logger.error(traceback.format_exc())
continue
# pred_x0_scaled is (batch_size, num_features, traj_length)
pred_x0_scaled_unpermuted = pred_x0_scaled.permute(0, 2, 1)
if debug_mode and logger and local_rank == 0:
logger.info(f"pred_x0_scaled_unpermuted stats before inverse_transform: min={pred_x0_scaled_unpermuted.min().item():.4f}, max={pred_x0_scaled_unpermuted.max().item():.4f}")
if (pred_x0_scaled_unpermuted < 0).any() or (pred_x0_scaled_unpermuted > 1).any():
if logger and local_rank == 0:
logger.warning(f"Batch {batch_idx} - Values outside [0,1] in pred_x0_scaled: min={pred_x0_scaled_unpermuted.min().item():.4f}, max={pred_x0_scaled_unpermuted.max().item():.4f}. Clamping.")
pred_x0_scaled_unpermuted = torch.clamp(pred_x0_scaled_unpermuted, 0, 1)
# Inverse transform to original data scale - ensure this happens on the correct device
pred_x0_final = scaler.inverse_transform(pred_x0_scaled_unpermuted)
ground_truth_final = testx_raw.cpu()
if torch.isnan(pred_x0_final).any() or torch.isnan(ground_truth_final).any():
if logger and local_rank == 0: logger.error(f"Batch {batch_idx} - NaN detected after inverse transform!")
continue
# Move to CPU before converting to NumPy for metric calculation
pred_x0_np = pred_x0_final.cpu().numpy()
ground_truth_np = ground_truth_final.numpy()
if debug_mode and logger and local_rank == 0:
logger.info(f"Shapes for metrics: pred_x0_np {pred_x0_np.shape}, ground_truth_np {ground_truth_np.shape}")
logger.info(f"pred_x0_np stats: min={np.min(pred_x0_np):.4f}, max={np.max(pred_x0_np):.4f}")
logger.info(f"ground_truth_np stats: min={np.min(ground_truth_np):.4f}, max={np.max(ground_truth_np):.4f}")
try:
mtd_list.append(mean_trajectory_deviation(pred_x0_np, ground_truth_np))
mppe_list.append(mean_point_to_point_error(pred_x0_np, ground_truth_np))
maepp_list.append(mean_absolute_error_per_point(pred_x0_np[:, :, 0], ground_truth_np[:, :, 0]))
maeps_list.append(mean_absolute_error_per_sample(pred_x0_np[:, :, 0], ground_truth_np[:, :, 0]))
aptc_result, avg_aptc_result = trajectory_coverage(pred_x0_np, ground_truth_np, thresholds)
aptc_list.append(aptc_result)
avg_aptc_list.append(avg_aptc_result)
max_td_list.append(max_trajectory_deviation(pred_x0_np, ground_truth_np))
except Exception as e:
if logger and local_rank == 0: logger.error(f"Exception during metric calculation in batch {batch_idx}: {str(e)}")
if debug_mode and logger and local_rank == 0: import traceback; logger.error(traceback.format_exc())
continue
if debug_mode and batch_idx == 0 and os.environ.get('PROJECT_DEBUG_MODE', '0') == '1': # Use a distinct env var for this specific break
if logger and local_rank == 0: logger.info("Project debug mode: Breaking after first test batch")
break
# Aggregate and log metrics (only on rank 0 if distributed)
if local_rank == 0:
mean_mtd = np.mean(mtd_list) if mtd_list else float('nan')
mean_mppe = np.mean(mppe_list) if mppe_list else float('nan')
mean_maepp = np.mean(maepp_list) if maepp_list else float('nan')
mean_maeps = np.mean(maeps_list) if maeps_list else float('nan')
mean_avg_aptc = np.mean(avg_aptc_list) if avg_aptc_list else float('nan')
mean_max_td = np.max(max_td_list) if max_td_list else float('nan') # MaxTD is max over all samples
mean_aptc_thresholds = {k: np.mean([d[k] for d in aptc_list if k in d]) for k in aptc_list[0]} if aptc_list else {f'TC@{thr}': float('nan') for thr in thresholds}
if logger:
logger.info(f"--- Test Results for Epoch {epoch} ({sampling_type.upper()}) ---")
logger.info(f"Mean MTD: {mean_mtd:.4f}")
logger.info(f"Mean MPPE: {mean_mppe:.4f}")
logger.info(f"Mean MAEPP (time): {mean_maepp:.4f}")
logger.info(f"Mean MAEPS (time): {mean_maeps:.4f}")
logger.info(f"Mean AVG_TC: {mean_avg_aptc:.4f}")
logger.info(f"Overall MaxTD: {mean_max_td:.4f}")
for threshold_val, tc_val in mean_aptc_thresholds.items():
logger.info(f"Mean {threshold_val}: {tc_val:.4f}")
if sampling_type == 'ddim':
logger.info(f"DDIM sampling with {ddim_steps} steps, eta: {ddim_eta:.2f}")
else:
logger.info(f"DDPM sampling with {config.diffusion.num_diffusion_timesteps} steps")
# Save results to .npy files
results_dir = exp_dir / 'results'
os.makedirs(results_dir, exist_ok=True)
sampling_prefix = f"{sampling_type.upper()}_"
def save_metric_npy(metric_name, value, current_epoch):
file_path = results_dir / f"{sampling_prefix}Test_mean_{metric_name}.npy"
if np.isnan(value): return # Don't save if NaN
if os.path.exists(file_path):
try:
existing_data = np.load(file_path, allow_pickle=True).item()
except: # Handle empty or corrupted file
existing_data = {}
existing_data[current_epoch] = value
else:
existing_data = {current_epoch: value}
np.save(file_path, existing_data)
save_metric_npy('mtd', mean_mtd, epoch)
save_metric_npy('mppe', mean_mppe, epoch)
save_metric_npy('maepp', mean_maepp, epoch)
save_metric_npy('maeps', mean_maeps, epoch)
save_metric_npy('avg_aptc', mean_avg_aptc, epoch)
save_metric_npy('max_td', mean_max_td, epoch)
for threshold_key, tc_value in mean_aptc_thresholds.items():
metric_key_name = threshold_key.replace('@', '_at_') # Sanitize for filename
save_metric_npy(f"tc_{metric_key_name}", tc_value, epoch)
if logger: logger.info(f"Saved test metrics to {results_dir}")
# Ensure all processes finish if in DDP, though testing is usually single-process or rank 0 handles results
if torch.distributed.is_initialized():
torch.distributed.barrier() # Wait for all processes if any were involved
return { # Return main metrics, could be useful for main script
"mean_mtd": mean_mtd,
"mean_mppe": mean_mppe
} if local_rank == 0 else {} |