Views
No views yet
| True \ Pred | DoS (pred) | Normal (pred) |
|---|---|---|
| DoS | 3,632,463 | 2,120 |
| Normal | 272,327 | 716,544 |
1import torch
2import numpy as np
3from can_defender_dos import CANLSTM # replace with your actual import
4
5# Example frame: [CAN_ID, b0, b1, ..., b7]
6frame = [0x315, 0x12, 0x4F, 0xA2, 0x00, 0x00, 0x78, 0x1C, 0xAA]
7
8# Convert to the same shape as the model expects: (batch_size, seq_len, features)
9x_np = np.array(frame, dtype=np.float32).reshape(1, 1, 9)
10
11model = CANLSTM(input_dim=9, hidden_dim=64, num_classes=2)
12model.load_state_dict(torch.load("candefender_dos_final.pt"))
13model.eval()
14
15with torch.no_grad():
16 logits = model(torch.from_numpy(x_np))
17 pred = torch.argmax(logits, dim=1).item()
18 print("Prediction:", "DoS" if pred == 0 else "Normal")