Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Resize the predictions to the original image size
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAB committed Jul 20, 2020
1 parent b99ac6d commit b4765ba
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 58 deletions.
5 changes: 2 additions & 3 deletions tf/run_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def run(input_path, output_path, model_path):
resize_method="upper_bound",
image_interpolation_method=cv2.INTER_CUBIC,
)

#normalize_image = NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])


def compose2(f1, f2):
return lambda x: f2(f1(x))

Expand All @@ -81,6 +79,7 @@ def compose2(f1, f2):
# compute
output = tf_rep.run(img_input.reshape(1, 3, 384, 384))
prediction = np.array(output).reshape(384, 384)
prediction = cv2.resize(prediction, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_CUBIC)

# output
filename = os.path.join(
Expand Down
105 changes: 50 additions & 55 deletions tf/run_pb.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,81 +24,76 @@ def run(input_path, output_path, model_path):
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
#tf.config.experimental.set_memory_growth(gpu, True)
tf.config.experimental.set_virtual_device_configuration(gpu,
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=4000)])
except RuntimeError as e:
print(e)

# select device
with tf.device('/gpu:0'):

# load network
graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile(model_path, 'rb') as f:
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')

#output_layer = '1195:0'
#input_node = '0:0'
model_operations = tf.compat.v1.get_default_graph().get_operations()
input_node = '0:0'
output_layer = model_operations[len(model_operations) - 1].name + ':0'
print("Last layer name: ", output_layer)

resize_image = Resize(
384,
384,
resize_target=None,
keep_aspect_ratio=False,
ensure_multiple_of=32,
resize_method="upper_bound",
image_interpolation_method=cv2.INTER_CUBIC,
)

#normalize_image = NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

def compose2(f1, f2):
return lambda x: f2(f1(x))
# load network
graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile(model_path, 'rb') as f:
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')


model_operations = tf.compat.v1.get_default_graph().get_operations()
input_node = '0:0'
output_layer = model_operations[len(model_operations) - 1].name + ':0'
print("Last layer name: ", output_layer)

resize_image = Resize(
384,
384,
resize_target=None,
keep_aspect_ratio=False,
ensure_multiple_of=32,
resize_method="upper_bound",
image_interpolation_method=cv2.INTER_CUBIC,
)

def compose2(f1, f2):
return lambda x: f2(f1(x))

transform = compose2(resize_image, PrepareForNet())
transform = compose2(resize_image, PrepareForNet())

# get input
img_names = glob.glob(os.path.join(input_path, "*"))
num_images = len(img_names)
# get input
img_names = glob.glob(os.path.join(input_path, "*"))
num_images = len(img_names)

# create output folder
os.makedirs(output_path, exist_ok=True)
# create output folder
os.makedirs(output_path, exist_ok=True)

print("start processing")
print("start processing")

with tf.compat.v1.Session() as sess:
try:
# load images
for ind, img_name in enumerate(img_names):
with tf.compat.v1.Session() as sess:
try:
# load images
for ind, img_name in enumerate(img_names):

print(" processing {} ({}/{})".format(img_name, ind + 1, num_images))
print(" processing {} ({}/{})".format(img_name, ind + 1, num_images))

# input
img = utils.read_image(img_name)
img_input = transform({"image": img})["image"]
# input
img = utils.read_image(img_name)
img_input = transform({"image": img})["image"]

# compute
prob_tensor = sess.graph.get_tensor_by_name(output_layer)
prediction, = sess.run(prob_tensor, {input_node: [img_input] })
prediction = prediction.reshape(384, 384)

# compute
prob_tensor = sess.graph.get_tensor_by_name(output_layer)
prediction, = sess.run(prob_tensor, {input_node: [img_input] })
prediction = prediction.reshape(384, 384)
prediction = cv2.resize(prediction, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_CUBIC)

# output
filename = os.path.join(
output_path, os.path.splitext(os.path.basename(img_name))[0]
)
utils.write_depth(filename, prediction, bits=2)

except KeyError:
print ("Couldn't find input node: ' + input_node + ' or output layer: " + output_layer + ".")
exit(-1)
except KeyError:
print ("Couldn't find input node: ' + input_node + ' or output layer: " + output_layer + ".")
exit(-1)

print("finished")
print("finished")


if __name__ == "__main__":
Expand Down

0 comments on commit b4765ba

Please sign in to comment.