-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.py
executable file
·51 lines (40 loc) · 1.36 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
from __future__ import absolute_import
import paddle.fluid as fluid
import paddle.fluid.io as io
import numpy as np
import sys
from PIL import Image
from keras.applications.vgg16 import decode_predictions
from vgg19 import VGG19
## Read image and preprocess
im = Image.open(sys.argv[1])
im = im.resize((224, 224), Image.ANTIALIAS)
im = np.array(im).astype(np.float32)
im = im.transpose((2, 0, 1)) # CHW
im = im[(2, 1, 0), :, :] # BGR
mean = np.array([104., 117., 124.], dtype=np.float32)
mean = mean.reshape([3, 1, 1])
im = im - mean
im = np.expand_dims(im, 0)
## Define the network
infer_program = fluid.default_main_program().clone(for_test=True)
with fluid.program_guard(infer_program):
inputs = fluid.layers.data('img', shape=[3, 224, 224])
predict = VGG19(include_top=True, infer=True).net(inputs)
## Create executor
# place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda() else fluid.CPUPlace()
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
## Load params
io.load_params(exe, "./vgg19_pd_params")
# ## Create inference program
# infer_program = fluid.default_main_program().clone(for_test=True)
# infer_program.prune(predict)
## Run it
p = exe.run(infer_program, fetch_list=[predict], feed={
'img': im
})
p = decode_predictions(p[0])
for (_, cls, prob) in p[0]:
print("{}: {}".format(cls, prob))