File size: 1,174 Bytes
77e3e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path

from torch_uncertainty.models.resnet import resnet
from safetensors.torch import load_file


def load_model(version: int):
    """Load the model corresponding to the given version."""
    model = resnet(
        arch=18,
        num_classes=200,
        in_channels=3,
        style="cifar",
        conv_bias=False,
    )
    path = Path(
        f"tiny-imagenet-resnet18/tiny-imagenet-resnet18-0-1023/version_{version}.safetensors"
    )
    if not path.exists():
        raise ValueError("File does not exist")

    state_dict = load_file(path)
    model.load_state_dict(state_dict=state_dict)
    return model

    from torch_uncertainty.datamodules.classification.tiny_imagenet import TinyImageNetDataModule
from torchmetrics import Accuracy

# Compute the accuracy using the first checkpoint
acc = Accuracy("multiclass", num_classes=200)
data_module = TinyImageNetDataModule(
    root="data",
    batch_size=32,
)
model = load_model(0)

model.eval()
data_module.setup("test")

for batch in data_module.test_dataloader()[0]:
    x, y = batch
    y_hat = model(x)
    acc.update(y_hat, y)
print(f"Accuracy on the test set: {acc.compute():.3%}")