-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtest_demo_any.py
33 lines (26 loc) · 1.33 KB
/
test_demo_any.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
'''
The script does inference (semantic segmentation) on arbitrary images.
Just drop some JPG files into demo_dir and run the script.
Results will be written into the same folder.
For better results channel_means better be recalculated I suppose. But it is kinda tricky.
'''
from os import path as osp
from glob import glob
import numpy as np
from model import DeepLab
from utils import ( save_load_means, subtract_channel_means, single_demo, read_image)
if __name__ == '__main__':
demo_dir = 'data/demos/deeplab/resnet_101_voc2012/'
models_dir = 'data/models/deeplab/resnet_101_voc2012/'
model_filename = 'resnet_101_0.6959.ckpt'
channel_means = save_load_means(means_filename='channel_means.npz',image_filenames=None, recalculate=False)
deeplab = DeepLab('resnet_101', training=False)
deeplab.load(osp.join(models_dir, model_filename))
files = glob(demo_dir+'*.jpg')
for image_filename in files:
filename=osp.basename(image_filename).split('.')[0]
image = read_image(image_filename=image_filename)
image_input = subtract_channel_means(image=image, channel_means=channel_means)
output = deeplab.test(inputs=[image_input], target_height=image.shape[0], target_width=image.shape[1])[0]
single_demo(image, np.argmax(output, axis=-1), demo_dir, filename)
deeplab.close()