o-laurent commited on
Commit
77e3e0a
1 Parent(s): b61225d

:books: Add and fix scripts

Browse files
README.md CHANGED
@@ -38,6 +38,6 @@ pip install torch-uncertainty
38
 
39
  ### Loading models
40
 
41
- The functions to load the models are available in `scripts`.
42
 
43
  **Any questions?** Please feel free to ask in the [GitHub Issues](https://github.com/ENSTA-U2IS-AI/torch-uncertainty/issues) or on our [Discord server](https://discord.gg/HMCawt5MJu).
 
38
 
39
  ### Loading models
40
 
41
+ The functions to load the models are available in `scripts`. The script corresponding to Tiny-ImageNet also contains a snippet to evaluate the accuracy of a downloaded model.
42
 
43
  **Any questions?** Please feel free to ask in the [GitHub Issues](https://github.com/ENSTA-U2IS-AI/torch-uncertainty/issues) or on our [Discord server](https://discord.gg/HMCawt5MJu).
scripts/cifar-10-resnet18/std_loading.py CHANGED
@@ -1,12 +1,13 @@
1
  from pathlib import Path
2
 
3
- from torch_uncertainty.models.resnet import resnet18
4
  from safetensors.torch import load_file
5
 
6
 
7
  def load_model(version: int):
8
  """Load the model corresponding to the given version."""
9
- model = resnet18(
 
10
  num_classes=10,
11
  in_channels=3,
12
  style="cifar",
 
1
  from pathlib import Path
2
 
3
+ from torch_uncertainty.models.resnet import resnet
4
  from safetensors.torch import load_file
5
 
6
 
7
  def load_model(version: int):
8
  """Load the model corresponding to the given version."""
9
+ model = resnet(
10
+ arch=18,
11
  num_classes=10,
12
  in_channels=3,
13
  style="cifar",
scripts/cifar10-resnet20-frn-silu/std_loading.py CHANGED
@@ -1,14 +1,15 @@
1
  from pathlib import Path
2
  from torch.nn import functional as F
3
 
4
- from torch_uncertainty.models.resnet import resnet20
5
  from torch_uncertainty.layers.filter_response_norm import FilterResponseNorm2d
6
  from safetensors.torch import load_file
7
 
8
 
9
  def load_model(version: int):
10
  """Load the model corresponding to the given version."""
11
- model = resnet20(
 
12
  num_classes=10,
13
  in_channels=3,
14
  style="cifar",
 
1
  from pathlib import Path
2
  from torch.nn import functional as F
3
 
4
+ from torch_uncertainty.models.resnet import resnet
5
  from torch_uncertainty.layers.filter_response_norm import FilterResponseNorm2d
6
  from safetensors.torch import load_file
7
 
8
 
9
  def load_model(version: int):
10
  """Load the model corresponding to the given version."""
11
+ model = resnet(
12
+ arch=20,
13
  num_classes=10,
14
  in_channels=3,
15
  style="cifar",
scripts/tiny-imagenet-resnet18/std_loading_testing.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ from torch_uncertainty.models.resnet import resnet
4
+ from safetensors.torch import load_file
5
+
6
+
7
+ def load_model(version: int):
8
+ """Load the model corresponding to the given version."""
9
+ model = resnet(
10
+ arch=18,
11
+ num_classes=200,
12
+ in_channels=3,
13
+ style="cifar",
14
+ conv_bias=False,
15
+ )
16
+ path = Path(
17
+ f"tiny-imagenet-resnet18/tiny-imagenet-resnet18-0-1023/version_{version}.safetensors"
18
+ )
19
+ if not path.exists():
20
+ raise ValueError("File does not exist")
21
+
22
+ state_dict = load_file(path)
23
+ model.load_state_dict(state_dict=state_dict)
24
+ return model
25
+
26
+ from torch_uncertainty.datamodules.classification.tiny_imagenet import TinyImageNetDataModule
27
+ from torchmetrics import Accuracy
28
+
29
+ # Compute the accuracy using the first checkpoint
30
+ acc = Accuracy("multiclass", num_classes=200)
31
+ data_module = TinyImageNetDataModule(
32
+ root="data",
33
+ batch_size=32,
34
+ )
35
+ model = load_model(0)
36
+
37
+ model.eval()
38
+ data_module.setup("test")
39
+
40
+ for batch in data_module.test_dataloader()[0]:
41
+ x, y = batch
42
+ y_hat = model(x)
43
+ acc.update(y_hat, y)
44
+ print(f"Accuracy on the test set: {acc.compute():.3%}")