|
1 |
| -import torch.nn |
2 |
| -import torchvision.models as models |
3 | 1 |
|
4 |
| -def noncompliant(): |
5 |
| - model = models.vgg16() |
6 |
| - model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant {{Set the module in training or evaluation mode.}} |
7 |
| - #^^^^^^^^^^^^^^^^^^^^^ |
8 |
| - ... |
| 2 | +import torchvision.models as models |
9 | 3 |
|
10 |
| -class CustomModule(torch.nn.Module): |
11 |
| - pass |
| 4 | +def torch_nn_imported(): |
| 5 | + import torch.nn |
| 6 | + def noncompliant(): |
| 7 | + model = models.vgg16() |
| 8 | + model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant {{Set the module in training or evaluation mode.}} |
| 9 | + #^^^^^^^^^^^^^^^^^^^^^ |
| 10 | + ... |
12 | 11 |
|
13 |
| -def noncompliant(): |
14 |
| - model = CustomModule() |
15 |
| - model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant |
16 |
| - ... |
| 12 | + class CustomModule(torch.nn.Module): |
| 13 | + pass |
17 | 14 |
|
18 |
| -def noncompliant(): |
19 |
| - model = models.vgg16() |
20 |
| - model.train() |
21 |
| - model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant |
| 15 | + def noncompliant(): |
| 16 | + model = CustomModule() |
| 17 | + model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant |
| 18 | + ... |
22 | 19 |
|
23 |
| -def noncompliant(): |
24 |
| - model = models.vgg16() |
25 |
| - model.load_state_dict(torch.load('model_weights.pth')) |
26 |
| - model.train() |
| 20 | + def noncompliant(): |
| 21 | + model = models.vgg16() |
| 22 | + model.train() |
| 23 | + model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant |
27 | 24 |
|
28 |
| - model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant |
| 25 | + def noncompliant(): |
| 26 | + model = models.vgg16() |
| 27 | + model.load_state_dict(torch.load('model_weights.pth')) |
| 28 | + model.train() |
29 | 29 |
|
30 |
| - other_model = model |
| 30 | + model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant |
31 | 31 |
|
32 |
| -def compliant(model): |
33 |
| - model.load_state_dict(torch.load('model_weights.pth')) |
| 32 | + other_model = model |
34 | 33 |
|
| 34 | + def compliant(model): |
| 35 | + model.load_state_dict(torch.load('model_weights.pth')) |
35 | 36 |
|
36 | 37 |
|
37 |
| -def compliant(): |
38 |
| - model3 = models.vgg16() |
39 |
| - model3.load_state_dict(torch.load('model_weights.pth')) # Ok if model is passed as argument to a function do not raise at all train or eval could be called in such functions |
40 |
| - foo(model3) |
41 | 38 |
|
| 39 | + def compliant(): |
| 40 | + model3 = models.vgg16() |
| 41 | + model3.load_state_dict(torch.load('model_weights.pth')) # Ok if model is passed as argument to a function do not raise at all train or eval could be called in such functions |
| 42 | + foo(model3) |
42 | 43 |
|
43 | 44 |
|
| 45 | +def torch_imported(): |
| 46 | + import torch |
44 | 47 |
|
| 48 | + class CustomModule(torch.nn.Module): |
| 49 | + pass |
45 | 50 |
|
| 51 | + def noncompliant(): |
| 52 | + model = CustomModule() |
| 53 | + model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant |
| 54 | + ... |
0 commit comments