-
Notifications
You must be signed in to change notification settings - Fork 37
/
infer.py
71 lines (58 loc) · 1.82 KB
/
infer.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
70
71
import oneflow as flow
import argparse
import numpy as np
import time
from models.vgg import vgg19_bn, vgg16_bn, vgg19, vgg16
from utils.imagenet1000_clsidx_to_labels import clsidx_2_labels
from utils.numpy_data_utils import load_image
model_dict = {
"vgg16": vgg16,
"vgg19": vgg19,
"vgg16_bn": vgg16_bn,
"vgg19_bn": vgg19_bn,
}
def _parse_args():
parser = argparse.ArgumentParser("flags for test vgg")
parser.add_argument(
"--model_path",
type=str,
default="vgg_imagenet_pretrain_model/",
help="model path",
)
parser.add_argument("--image_path", type=str, default="", help="input image path")
parser.add_argument(
"--model",
type=str,
default="vgg19_bn",
help="choose from vgg16, vgg19, vgg16_bn, vgg19_bn",
)
return parser.parse_args()
def main(args):
assert args.model in model_dict
print("Predicting using", args.model, "...")
start_t = time.time()
vgg_module = model_dict[args.model]()
end_t = time.time()
print("init time : {}".format(end_t - start_t))
start_t = time.time()
pretrain_models = flow.load(args.model_path)
vgg_module.load_state_dict(pretrain_models)
end_t = time.time()
print("load params time : {}".format(end_t - start_t))
vgg_module.eval()
vgg_module.to("cuda")
start_t = time.time()
image = load_image(args.image_path)
image = flow.Tensor(image, device=flow.device("cuda"))
predictions = vgg_module(image).softmax()
predictions = predictions.numpy()
end_t = time.time()
print("infer time : {}".format(end_t - start_t))
clsidx = np.argmax(predictions)
print(
"predict prob: %f, class name: %s"
% (np.max(predictions), clsidx_2_labels[clsidx])
)
if __name__ == "__main__":
args = _parse_args()
main(args)