-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
63 lines (51 loc) · 1.98 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
import os
from argparse import ArgumentParser
import cv2
import numpy as np
import torch
import torchvision.transforms as T
from training import Classifier
class Predictor:
"""Single model predictor."""
def __init__(self, checkpoint_path: str, force_cpu: bool = False):
"""Initialize the model from a checkpoint."""
self.device = "cuda" if torch.cuda.is_available() and not force_cpu else "cpu"
print(
f"Loading model from checkpoint: {checkpoint_path} \n to device: {self.device}"
)
self.model = Classifier.load_from_checkpoint(
checkpoint_path,
map_location=self.device,
)
self.model.eval()
self.model.freeze()
self.model.to(self.device)
self.tf = T.Compose([T.ToTensor(), T.Normalize((0.1307,), (0.3081,))])
@torch.no_grad()
def __call__(self, imgs: list):
"""Forward pass for a batch."""
tensors = []
for img in imgs:
if not isinstance(img, np.ndarray):
raise TypeError("all images must be numpy.ndarray")
if not len(img.shape) == 2:
raise TypeError("all images must have the shape [H, W]")
tensors.append(self.tf(img))
tensors = torch.stack(tensors, dim=0).to(self.device)
outputs = self.model(tensors).softmax(dim=-1)
return outputs.cpu()
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--checkpoint", type=str, required=True)
parser.add_argument("--image", type=str, required=True)
args = parser.parse_args()
if not os.path.exists(args.image):
raise RuntimeError("Invalid path specified")
mnistmodel = Predictor(args.checkpoint)
image = cv2.imread(args.image)
if len(image.shape) == 3:
image = image[0] # only take first channel
outputs = mnistmodel([image])
print(
f"Inference results for {args.image}:\n {outputs} \n Prediction result:\n {np.argmax(outputs)+1}"
)