-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodelopera.py
37 lines (31 loc) · 928 Bytes
/
modelopera.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
# coding=utf-8
import torch
from network import img_network
def get_fea(args):
if args.net.startswith('vgg'):
net = img_network.VGGBase(args)
elif args.net == 'LeNet':
net = img_network.LeNetBase()
elif args.net == 'DTN':
net = img_network.DTNBase()
elif args.net.startswith('res'):
net = img_network.ResBase(args)
else:
net = img_network.VGGBase(args)
return net
def accuracy(network, loader):
correct = 0
total = 0
network.eval()
with torch.no_grad():
for data in loader:
x = data[0].cuda().float()
y = data[1].cuda().long()
p = network(x)
if p.size(1) == 1:
correct += (p.gt(0).eq(y).float()).sum().item()
else:
correct += (p.argmax(1).eq(y).float()).sum().item()
total += len(x)
network.train()
return correct / total