-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtest_demo.py
69 lines (54 loc) · 2.1 KB
/
test_demo.py
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
62
63
64
65
66
67
68
69
"""
Test Demo
```bash
python test_demo.py --im_path=data/I03_01_1.bmp
```
Date: 2018/5/26
"""
from argparse import ArgumentParser
import torch
from torch import nn
import torch.nn.functional as F
from PIL import Image
from IQADataset import NonOverlappingCropPatches
class CNNIQAnet(nn.Module):
def __init__(self, ker_size=7, n_kers=50, n1_nodes=800, n2_nodes=800):
super(CNNIQAnet, self).__init__()
self.conv1 = nn.Conv2d(1, n_kers, ker_size)
self.fc1 = nn.Linear(2 * n_kers, n1_nodes)
self.fc2 = nn.Linear(n1_nodes, n2_nodes)
self.fc3 = nn.Linear(n2_nodes, 1)
self.dropout = nn.Dropout()
def forward(self, x):
x = x.view(-1, x.size(-3), x.size(-2), x.size(-1)) #
h = self.conv1(x)
# h1 = F.adaptive_max_pool2d(h, 1)
# h2 = -F.adaptive_max_pool2d(-h, 1)
h1 = F.max_pool2d(h, (h.size(-2), h.size(-1)))
h2 = -F.max_pool2d(-h, (h.size(-2), h.size(-1)))
h = torch.cat((h1, h2), 1) # max-min pooling
h = h.squeeze(3).squeeze(2)
h = F.relu(self.fc1(h))
h = self.dropout(h)
h = F.relu(self.fc2(h))
q = self.fc3(h)
return q
if __name__ == "__main__":
parser = ArgumentParser(description='PyTorch CNNIQA test demo')
parser.add_argument("--im_path", type=str, default='data/I03_01_1.bmp',
help="image path")
parser.add_argument("--model_file", type=str, default='models/CNNIQA-LIVE',
help="model file (default: models/CNNIQA-LIVE)")
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = CNNIQAnet(ker_size=7,
n_kers=50,
n1_nodes=800,
n2_nodes=800).to(device)
model.load_state_dict(torch.load(args.model_file))
im = Image.open(args.im_path).convert('L')
patches = NonOverlappingCropPatches(im, 32, 32)
model.eval()
with torch.no_grad():
patch_scores = model(torch.stack(patches).to(device))
print(patch_scores.mean().item())