-
Notifications
You must be signed in to change notification settings - Fork 3
/
QModel.py
executable file
·69 lines (50 loc) · 1.66 KB
/
QModel.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
#
#Copyright (C) ISTI-CNR
#Licensed under the BSD 3-Clause Clear License (see license.txt)
#
import os
import re
import glob2
import torch
from model import QNet
class QModel:
def __init__(self, run, grayscale = True):
self.run = run
if run.endswith('.pth'):
ckpt = run
else:
ckpt_dir = os.path.join(run, 'ckpt')
ckpts = glob2.glob(os.path.join(ckpt_dir, '*.pth'))
assert ckpts, "No checkpoints to resume from!"
def get_epoch(ckpt_url):
s = re.findall("ckpt_e(\d+).pth", ckpt_url)
epoch = int(s[0]) if s else -1
return epoch, ckpt_url
start_epoch, ckpt = max(get_epoch(c) for c in ckpts)
print('Checkpoint:', ckpt)
if torch.cuda.is_available():
ckpt = torch.load(ckpt)
else:
ckpt = torch.load(ckpt, map_location=torch.device('cpu'))
if grayscale:
n_in =1
else:
n_in = 3
model = QNet(n_in, 1)
if torch.cuda.is_available():
model = model.cuda()
model.load_state_dict(ckpt['model'])
model.eval()
self.model = model
def getModel():
return self.model
def predict(self, stim):
sz = stim.shape
if len(sz) == 3:
stim = stim.unsqueeze(0)
with torch.no_grad():
if torch.cuda.is_available():
stim = stim.cuda()
out = self.model(stim)
out = out.data.cpu().numpy().squeeze()
return out