forked from Vladkryvoruchko/PSPNet-Keras-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpspnet.py
executable file
·222 lines (183 loc) · 9.01 KB
/
pspnet.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
from __future__ import print_function
import os
from os.path import splitext, join, isfile, isdir, basename
import argparse
import numpy as np
from scipy import misc, ndimage
from keras import backend as K
from keras.models import model_from_json, load_model
import tensorflow as tf
import layers_builder as layers
from glob import glob
from python_utils import utils
from python_utils.preprocessing import preprocess_img
from keras.utils.generic_utils import CustomObjectScope
# These are the means for the ImageNet pretrained ResNet
DATA_MEAN = np.array([[[123.68, 116.779, 103.939]]]) # RGB order
class PSPNet(object):
"""Pyramid Scene Parsing Network by Hengshuang Zhao et al 2017"""
def __init__(self, nb_classes, resnet_layers, input_shape, weights):
self.input_shape = input_shape
json_path = join("weights", "keras", weights + ".json")
h5_path = join("weights", "keras", weights + ".h5")
if 'pspnet' in weights:
if os.path.isfile(json_path) and os.path.isfile(h5_path):
print("Keras model & weights found, loading...")
with CustomObjectScope({'Interp': layers.Interp}):
with open(json_path, 'r') as file_handle:
self.model = model_from_json(file_handle.read())
self.model.load_weights(h5_path)
else:
print("No Keras model & weights found, import from npy weights.")
self.model = layers.build_pspnet(nb_classes=nb_classes,
resnet_layers=resnet_layers,
input_shape=self.input_shape)
self.set_npy_weights(weights)
else:
print('Load pre-trained weights')
self.model = load_model(weights)
def predict(self, img, flip_evaluation=False):
"""
Predict segementation for an image.
Arguments:
img: must be rowsxcolsx3
"""
h_ori, w_ori = img.shape[:2]
# Preprocess
img = misc.imresize(img, self.input_shape)
img = img - DATA_MEAN
img = img[:, :, ::-1] # RGB => BGR
img = img.astype('float32')
print("Predicting...")
probs = self.feed_forward(img, flip_evaluation)
if img.shape[0:1] != self.input_shape: # upscale prediction if necessary
h, w = probs.shape[:2]
probs = ndimage.zoom(probs, (1. * h_ori / h, 1. * w_ori / w, 1.),
order=1, prefilter=False)
print("Finished prediction...")
return probs
def feed_forward(self, data, flip_evaluation=False):
assert data.shape == (self.input_shape[0], self.input_shape[1], 3)
if flip_evaluation:
print("Predict flipped")
input_with_flipped = np.array(
[data, np.flip(data, axis=1)])
prediction_with_flipped = self.model.predict(input_with_flipped)
prediction = (prediction_with_flipped[
0] + np.fliplr(prediction_with_flipped[1])) / 2.0
else:
prediction = self.model.predict(np.expand_dims(data, 0))[0]
return prediction
def set_npy_weights(self, weights_path):
npy_weights_path = join("weights", "npy", weights_path + ".npy")
json_path = join("weights", "keras", weights_path + ".json")
h5_path = join("weights", "keras", weights_path + ".h5")
print("Importing weights from %s" % npy_weights_path)
weights = np.load(npy_weights_path, encoding='bytes').item()
for layer in self.model.layers:
print(layer.name)
if layer.name[:4] == 'conv' and layer.name[-2:] == 'bn':
mean = weights[layer.name.encode()][
'mean'.encode()].reshape(-1)
variance = weights[layer.name.encode()][
'variance'.encode()].reshape(-1)
scale = weights[layer.name.encode()][
'scale'.encode()].reshape(-1)
offset = weights[layer.name.encode()][
'offset'.encode()].reshape(-1)
self.model.get_layer(layer.name).set_weights(
[scale, offset, mean, variance])
elif layer.name[:4] == 'conv' and not layer.name[-4:] == 'relu':
try:
weight = weights[layer.name.encode()]['weights'.encode()]
self.model.get_layer(layer.name).set_weights([weight])
except Exception as err:
biases = weights[layer.name.encode()]['biases'.encode()]
self.model.get_layer(layer.name).set_weights([weight,
biases])
print('Finished importing weights.')
print("Writing keras model & weights")
json_string = self.model.to_json()
with open(json_path, 'w') as file_handle:
file_handle.write(json_string)
self.model.save_weights(h5_path)
print("Finished writing Keras model & weights")
class PSPNet50(PSPNet):
"""Build a PSPNet based on a 50-Layer ResNet."""
def __init__(self, nb_classes, weights, input_shape):
PSPNet.__init__(self, nb_classes=nb_classes, resnet_layers=50,
input_shape=input_shape, weights=weights)
class PSPNet101(PSPNet):
"""Build a PSPNet based on a 101-Layer ResNet."""
def __init__(self, nb_classes, weights, input_shape):
PSPNet.__init__(self, nb_classes=nb_classes, resnet_layers=101,
input_shape=input_shape, weights=weights)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--model', type=str, default='pspnet101_voc2012',
help='Model/Weights to use',
choices=['pspnet50_ade20k',
'pspnet101_cityscapes',
'pspnet101_voc2012'])
parser.add_argument('-w', '--weights', type=str, default=None)
parser.add_argument('-i', '--input_path', type=str, default='example_images/ade20k.jpg',
help='Path the input image')
parser.add_argument('-g', '--glob_path', type=str, default=None,
help='Glob path for multiple images')
parser.add_argument('-o', '--output_path', type=str, default='example_results/ade20k.jpg',
help='Path to output')
parser.add_argument('--id', default="0")
parser.add_argument('--input_size', type=int, default=500)
parser.add_argument('-f', '--flip', type=bool, default=True,
help="Whether the network should predict on both image and flipped image.")
args = parser.parse_args()
# Handle input and output args
images = glob(args.glob_path) if args.glob_path else [args.input_path,]
if args.glob_path:
fn, ext = splitext(args.output_path)
if ext:
parser.error("output_path should be a folder for multiple file input")
if not isdir(args.output_path):
os.mkdir(args.output_path)
# Predict
os.environ["CUDA_VISIBLE_DEVICES"] = args.id
sess = tf.Session()
K.set_session(sess)
with sess.as_default():
print(args)
if not args.weights:
if "pspnet50" in args.model:
pspnet = PSPNet50(nb_classes=150, input_shape=(473, 473),
weights=args.model)
elif "pspnet101" in args.model:
if "cityscapes" in args.model:
pspnet = PSPNet101(nb_classes=19, input_shape=(713, 713),
weights=args.model)
if "voc2012" in args.model:
pspnet = PSPNet101(nb_classes=21, input_shape=(473, 473),
weights=args.model)
else:
print("Network architecture not implemented.")
else:
pspnet = PSPNet50(nb_classes=2, input_shape=(
768, 480), weights=args.weights)
for i, img_path in enumerate(images):
print("Processing image {} / {}".format(i+1,len(images)))
img = misc.imread(img_path, mode='RGB')
cimg = misc.imresize(img, (args.input_size, args.input_size))
probs = pspnet.predict(img, args.flip)
cm = np.argmax(probs, axis=2)
pm = np.max(probs, axis=2)
color_cm = utils.add_color(cm)
# color cm is [0.0-1.0] img is [0-255]
alpha_blended = 0.5 * color_cm * 255 + 0.5 * img
if args.glob_path:
input_filename, ext = splitext(basename(img_path))
filename = join(args.output_path, input_filename)
else:
filename, ext = splitext(args.output_path)
misc.imsave(filename + "_seg_read" + ext, cm)
misc.imsave(filename + "_seg" + ext, color_cm)
misc.imsave(filename + "_probs" + ext, pm)
misc.imsave(filename + "_seg_blended" + ext, alpha_blended)