-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinference.py
65 lines (48 loc) · 1.75 KB
/
inference.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
# !/usr/bin/env python3
"""
验证训练模型。
Author: pankeyu
Date: 2022/05/19
"""
import os
import numpy as np
import torch
import torch.nn as nn
from torchvision import models
import torch.nn.functional as F
from utils import RotNetDataset
input_shape = (3, 244, 244)
class RotateNet(nn.Module):
def __init__(self):
super().__init__()
self.model = models.resnet50(pretrained=True)
self.model.fc = nn.Linear(2048, 360)
def forward(self, x):
"""
前向传播,使用resnet 50作为backbone,后面接一个线性层。
Args:
x (_type_): (batch, 3, 224, 224)
Returns:
_type_: 360维的一个tensor,表征属于每一个角度类别的概率
"""
x = self.model(x)
return x
if __name__ == '__main__':
with torch.no_grad():
model = torch.load('models/model_13.pth', map_location=torch.device('cpu')).eval()
data_path = './img_examples'
files = os.listdir(data_path)
labels = [file.split('.')[0].split('_')[1] for file in files]
files = [os.path.join(data_path, f) for f in files]
val_dataset = RotNetDataset(files, input_shape=input_shape, rotate=False)
val_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size=64)
for (img, rotate_angle), label in zip(val_dataloader, labels):
img = img.float()
logits = model(img)
logits = F.softmax(logits, dim=-1)
pred = np.argmax(logits.numpy(), axis=1)
avg_diff = 0
for p, l in zip(pred, labels):
print('Infer / Label: {} / {}'.format(p, l))
avg_diff += abs(int(p) - int(l))
print('Avg diff: {:.2f}'.format(avg_diff / len(pred)))