-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
31 lines (28 loc) · 893 Bytes
/
predict.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
from models import CNN
from torchvision import transforms
import torch
from skimage import io
class Predictor:
def __init__(self, ckpt_path):
super().__init__()
self.transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(256),
transforms.ToTensor()
])
self.checkpoint = torch.load(ckpt_path)
self.model = CNN()
self.model.load_state_dict(state_dict=self.checkpoint['state_dict'])
self.use_cuda = False
self.model.eval()
if torch.cuda.is_available():
self.model.cuda()
self.use_cuda = True
def run(self, path):
img = self.transform(io.imread(path))
img.unsqueeze_(0)
if self.use_cuda:
img = img.cuda()
with torch.no_grad():
pred = self.model(img)
return pred.item()