sombruxo

Untitled

Feb 9th, 2026 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | Source Code | 0 0
  1. import torch
  2. import numpy as np
  3.  
  4. class PilotNet(torch.nn.Module):
  5.     def __init__(self):
  6.         super(PilotNet, self).__init__()
  7.         # Capas convolucionales con BatchNorm
  8.         self.conv1 = torch.nn.Conv2d(3, 24, 5, stride=2)
  9.         self.bn1 = torch.nn.BatchNorm2d(24)
  10.        
  11.         self.conv2 = torch.nn.Conv2d(24, 36, 5, stride=2)
  12.         self.bn2 = torch.nn.BatchNorm2d(36)
  13.        
  14.         self.conv3 = torch.nn.Conv2d(36, 48, 5, stride=2)
  15.         self.bn3 = torch.nn.BatchNorm2d(48)
  16.        
  17.         self.conv4 = torch.nn.Conv2d(48, 64, 3)
  18.         self.bn4 = torch.nn.BatchNorm2d(64)
  19.        
  20.         self.conv5 = torch.nn.Conv2d(64, 64, 3)
  21.         self.bn5 = torch.nn.BatchNorm2d(64)
  22.        
  23.         # Capas fully connected con BatchNorm
  24.         self.fc1 = torch.nn.Linear(1152, 100)
  25.         self.bn_fc1 = torch.nn.BatchNorm1d(100)
  26.        
  27.         self.fc2 = torch.nn.Linear(100, 50)
  28.         self.bn_fc2 = torch.nn.BatchNorm1d(50)
  29.        
  30.         self.fc3 = torch.nn.Linear(50, 10)
  31.         self.fc4 = torch.nn.Linear(10, 1)
  32.        
  33.     def forward(self, x):
  34.         x = torch.relu(self.bn1(self.conv1(x)))
  35.         x = torch.relu(self.bn2(self.conv2(x)))
  36.         x = torch.relu(self.bn3(self.conv3(x)))
  37.         x = torch.relu(self.bn4(self.conv4(x)))
  38.         x = torch.relu(self.bn5(self.conv5(x)))
  39.        
  40.         x = x.view(x.size(0), -1)
  41.        
  42.         x = torch.relu(self.bn_fc1(self.fc1(x)))
  43.         x = torch.relu(self.bn_fc2(self.fc2(x)))
  44.         x = torch.relu(self.fc3(x))
  45.         x = self.fc4(x)
  46.         return x
  47.  
  48. print("Cargando checkpoint...")
  49. checkpoint = torch.load('pilotnet_weights.pth')
  50.  
  51. print("Información del entrenamiento:")
  52. print(f"  Epoch: {checkpoint['epoch']}")
  53. print(f"  Normalización: {checkpoint.get('normalization_params')}")
  54. print(f"  Torque min: {checkpoint.get('min_torque_ref')}")
  55. print(f"  Torque max: {checkpoint.get('max_torque_ref')}")
  56. print(f"  Dataset: {checkpoint.get('data_file')}")
  57. print(f"  Error de test: {checkpoint.get('test_er')}")
  58.  
  59. # Carga el modelo
  60. model = PilotNet().cuda()
  61. model.load_state_dict(checkpoint['model_state_dict'])
  62. model.eval()
  63.  
  64. print("\n✓ Modelo cargado correctamente en GPU")
  65.  
  66. # Test rápido
  67. test_input = torch.randn(1, 3, 66, 200).cuda()
  68. with torch.no_grad():
  69.     output = model(test_input)
  70. print(f"✓ Inferencia OK - Output de prueba: {output.cpu().item():.4f}")
  71.  
  72. # ← PEGA AQUÍ EL CÓDIGO DE DIAGNÓSTICO ↓
  73.  
  74. print("\n--- DIAGNÓSTICO DEL MODELO ---")
  75.  
  76. # 1. Verifica rango de salida
  77. test_inputs = torch.randn(100, 3, 66, 200).cuda()
  78. with torch.no_grad():
  79.     outputs = model(test_inputs)
  80.  
  81. outputs_cpu = outputs.cpu().numpy()
  82. print(f"Rango de salidas del modelo:")
  83. print(f"  Min: {outputs_cpu.min():.4f}")
  84. print(f"  Max: {outputs_cpu.max():.4f}")
  85. print(f"  Mean: {outputs_cpu.mean():.4f}")
  86. print(f"  Std: {outputs_cpu.std():.4f}")
  87.  
  88. print(f"\nRango esperado según checkpoint:")
  89. print(f"  Min torque: {checkpoint.get('min_torque_ref')}")
  90. print(f"  Max torque: {checkpoint.get('max_torque_ref')}")
  91.  
  92. # 2. Verifica si las salidas están en rango razonable
  93. if checkpoint.get('min_torque_ref') and checkpoint.get('max_torque_ref'):
  94.     min_ref = checkpoint['min_torque_ref']
  95.     max_ref = checkpoint['max_torque_ref']
  96.    
  97.     in_range = ((outputs_cpu >= min_ref) & (outputs_cpu <= max_ref)).mean() * 100
  98.     print(f"\n% de predicciones dentro del rango: {in_range:.1f}%")
  99.    
  100.     if in_range < 50:
  101.         print("⚠️  ADVERTENCIA: Muchas predicciones fuera de rango esperado")
  102.     else:
  103.         print("✓ Predicciones en rango razonable")
Advertisement
Add Comment
Please, Sign In to add comment