File size: 2,105 Bytes
320e2b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn.functional as F

from model import CNN_PCAw_SSRPMS_KAN
from classes import ESC50_CLASSES
from compression.pruning import apply_structural_pruning
from compression.qat import prepare_qat_model, convert_qat_model

def load_model(model_path: str, device: torch.device, num_classes: int):
    model = CNN_PCAw_SSRPMS_KAN(num_classes=num_classes)

    checkpoint = torch.load(model_path, map_location=device)

    # Case 1: pure state_dict saved with torch.save(model.state_dict(), path)
    try:
        model.load_state_dict(checkpoint)
    except RuntimeError:
        raise ValueError("Unsupported checkpoint format.")

    model.to(device)
    model.eval()
    return model

def load_compressed_model(model_path: str, compressed_model_path: str, num_classes: int):
    model = CNN_PCAw_SSRPMS_KAN(num_classes=num_classes)

    original_checkpoint = torch.load(model_path, map_location="cpu")
    compressed_checkpoint = torch.load(compressed_model_path, map_location="cpu")
    try:
        model.load_state_dict(original_checkpoint)
        model = apply_structural_pruning(model)
        model = prepare_qat_model(model)
        model = convert_qat_model(model)
        model.load_state_dict(compressed_checkpoint)
    except RuntimeError as e:
        print(f"Error loading compressed model: {e}")
        raise ValueError("Unsupported checkpoint format.")

    model.to("cpu")
    model.eval()
    return model

@torch.no_grad()
def predict(model, input_tensor: torch.Tensor, device: torch.device):
    input_tensor = input_tensor.to(device)

    logits = model(input_tensor)
    probs = F.softmax(logits, dim=1).squeeze(0).cpu()

    top_idx = torch.argmax(probs).item()
    top_class = ESC50_CLASSES[top_idx]
    top_prob = probs[top_idx].item()

    all_probs = [
        {"class_name": ESC50_CLASSES[i], "probability": float(probs[i])}
        for i in range(len(ESC50_CLASSES))
    ]

    all_probs = sorted(all_probs, key=lambda x: x["probability"], reverse=True)

    return top_class, top_prob, all_probs