Inference problems when runing with a set of images
Good morning,
When I try to run inference on every image of a folder:
python model_inspect.py --runmode=infer --model_name=$MODEL --max_boxes_to_draw=100 --min_score_thresh=0.2 --ckpt_path=$CKPT_PATH --input_image=../../bdd100k/images/10k/test/*.jpg --output_image_dir=/tmp --enable_ema=False
I got the following error:
Traceback (most recent call last):
File "model_inspect.py", line 309, in <module>
tf.app.run(main)
File "/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/platform/app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "/opt/conda/lib/python3.7/site-packages/absl/app.py", line 299, in run
_run_main(main, args)
File "/opt/conda/lib/python3.7/site-packages/absl/app.py", line 250, in _run_main
sys.exit(main(argv))
File "model_inspect.py", line 304, in main
inspector.run_model(FLAGS.runmode, FLAGS.threads)
File "model_inspect.py", line 282, in run_model
self.inference_single_image(FLAGS.input_image, FLAGS.output_image_dir)
File "model_inspect.py", line 170, in inference_single_image
driver.inference(image_image_path, output_dir)
File "/workspace/vortiz/igpu04/vortiz/automl/efficientdet/inference.py", line 244, in inference
self.model_name, images, **self.params)
File "/workspace/vortiz/igpu04/vortiz/automl/efficientdet/inference.py", line 109, in build_model
class_outputs, box_outputs = model_arch(inputs, model_name, **kwargs)
File "/workspace/vortiz/igpu04/vortiz/automl/efficientdet/efficientdet_arch.py", line 554, in efficientdet
*utils.num_params_flops()))
File "/workspace/vortiz/igpu04/vortiz/automl/efficientdet/utils.py", line 269, in num_params_flops
tf.get_default_graph(), options=options).total_float_ops
File "/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/profiler/model_analyzer.py", line 354, in profile
graph_str = _graph_string(graph)
File "/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/profiler/model_analyzer.py", line 54, in _graph_string
return graph.as_graph_def(add_shapes=True).SerializeToString()
File "/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 3134, in as_graph_def
result, _ = self._as_graph_def(from_version, add_shapes)
File "/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 3051, in _as_graph_def
graph.ParseFromString(compat.as_bytes(data))
google.protobuf.message.DecodeError: Error parsing message
Does anyone know what's going on? Thank you so much.
No problem now, but I see that all the images are passed as a batch (thus having to fit into the GPU's memory not to fail). Therefore, which is the proper way to take all images of a dataset (i.e. thousands of images) and predict them one by one?
Thank you.
Yes, I have the same problem, it'd be great if we could run inference for multiple images.
Yes, I have the same problem, it'd be great if we could run inference for multiple images.
You should try ServingDriver at inference.py
Yes, I have the same problem, it'd be great if we could run inference for multiple images.
You should try ServingDriver at inference.py
Thanks for the tip, I'll definitely try.
Documentation is a bit misleading, but I managed to make a running code. It still needs a bit of polishing, but works with batch_size=1. For other batch sizes, the number of input images should be changed:
imgs = []
image_path_pattern = 'testdata/*.jpg'
output_dir = 'testdata/output'
image_size = [1280, 720]
batch_size = 1
min_score_thresh = 0.5
max_boxes_to_draw = 100
# read input images
for f in tf.io.gfile.glob(image_path_pattern):
imgs.append(np.array(Image.open(f)))
# set up driver with given parameters
driver = inference.ServingDriver(
'efficientdet-d0', '/tmp/ckpt/efficientdet-d0', batch_size=batch_size)
driver.build(params_override=dict(image_size=image_size), min_score_thresh=min_score_thresh, max_boxes_to_draw=max_boxes_to_draw)
# run inference on each image, visualize and save output
for i, input in enumerate(imgs):
predictions = driver.serve_images([input])
img = driver.visualize(input, predictions[0],
line_thickness=1)
output_image_path = os.path.join(output_dir, str(i) + '.jpg')
Image.fromarray(img).save(output_image_path)
@mingxingtan Could you correct the ServingDriver class' description? The examples show to use serve_files, but instead serve_images should be used in that context. Otherwise, it was a great help!
Yeah, sure:
import inference
import numpy as np
from PIL import Image
import tensorflow as tf
import os
Documentation is a bit misleading, but I managed to make a running code. It still needs a bit of polishing, but works with
batch_size=1. For other batch sizes, the number of input images should be changed:imgs = [] image_path_pattern = 'testdata/*.jpg' output_dir = 'testdata/output' image_size = [1280, 720] batch_size = 1 min_score_thresh = 0.5 max_boxes_to_draw = 100 # read input images for f in tf.io.gfile.glob(image_path_pattern): imgs.append(np.array(Image.open(f))) # set up driver with given parameters driver = inference.ServingDriver( 'efficientdet-d0', '/tmp/ckpt/efficientdet-d0', batch_size=batch_size) driver.build(params_override=dict(image_size=image_size), min_score_thresh=min_score_thresh, max_boxes_to_draw=max_boxes_to_draw) # run inference on each image, visualize and save output for i, input in enumerate(imgs): predictions = driver.serve_images([input]) img = driver.visualize(input, predictions[0], line_thickness=1) output_image_path = os.path.join(output_dir, str(i) + '.jpg') Image.fromarray(img).save(output_image_path)@mingxingtan Could you correct the ServingDriver class' description? The examples show to use
serve_files, but insteadserve_imagesshould be used in that context. Otherwise, it was a great help!
Hello, and thank you very much for your code snippet. I have modified it in order to get the algorithm througput, but the result was very disappointing, as I got a mean forward pass time as bad as 1.4 secons on a NVidia V100 GPU. Here is the modified code:
imgs, elapsed_list = [], []
image_path_pattern = '/workspace/vortiz/resized_img_8/*.png'
output_dir = 'testdata/output'
image_size = [640, 480]
batch_size = 1
min_score_thresh = 0.4
max_boxes_to_draw = 60
# read input images
for f in tf.io.gfile.glob(image_path_pattern):
imgs.append(np.array(Image.open(f)))
# set up driver with given parameters
driver = inference.ServingDriver(
'efficientdet-d0', 'efficientdet-d0', batch_size=batch_size)
driver.build(params_override=dict(image_size=image_size), min_score_thresh=min_score_thresh, max_boxes_to_draw=max_boxes_to_draw)
# run inference on each image, visualize and save output
for i, input in enumerate(imgs):
start_time = time.time()
predictions = driver.serve_images([input])
img = driver.visualize(input, predictions[0],
line_thickness=1)
output_image_path = os.path.join(output_dir, str(i) + '.jpg')
Image.fromarray(img).save(output_image_path)
elapsed_time = time.time() - start_time
elapsed_list.append(elapsed_time)
print("--- %s seconds ---" % elapsed_time)
print("Mean elapsed time:", sum(elapsed_list)/len(elapsed_list))
Do you have any idea on what's going on?
Maybe you should measure the time of the inference itself and exclude the visualization and image saving, if that's not important for you. Also, you could try for a bigger batch size and modify the enumeration accordingly.
This only measures the inference:
# run inference on each image, visualize and save output
for i, input in enumerate(imgs):
start_time = time.time()
predictions = driver.serve_images([input])
elapsed_time = time.time() - start_time
img = driver.visualize(input, predictions[0],
line_thickness=1)
output_image_path = os.path.join(output_dir, str(i) + '.jpg')
Image.fromarray(img).save(output_image_path)
elapsed_list.append(elapsed_time)
print("--- %s seconds ---" % elapsed_time)
print("Mean elapsed time:", sum(elapsed_list)/len(elapsed_list))
Yeah, sure:
import inference import numpy as np from PIL import Image import tensorflow as tf import os
Yeah, I already guessed ;)
elapsed_time = time.time() - start_time
This is a kind of magic, as I was about to auto-answer myself with these considerations :smile:
I supposed that the slow performance is due to the image saving process and visualisation.
Thank you again! Vicent
elapsed_time = time.time() - start_time
This is a kind of magic, as I was about to auto-answer myself with these considerations
I supposed that the slow performance is due to the image saving process and visualisation.
Thank you again! Vicent
I'm glad I was able to help ;)
elapsed_time = time.time() - start_time
This is a kind of magic, as I was about to auto-answer myself with these considerations I supposed that the slow performance is due to the image saving process and visualisation. Thank you again! Vicent
I'm glad I was able to help ;)
Sooo... I have tried and now I got...
--- 8.912771463394165 seconds ---
--- 1.2804157733917236 seconds ---
--- 1.0804352760314941 seconds ---
--- 1.0525870323181152 seconds ---
--- 1.1890869140625 seconds ---
--- 1.0467119216918945 seconds ---
--- 0.7386507987976074 seconds ---
--- 0.6940393447875977 seconds ---
--- 1.55003023147583 seconds ---
--- 0.9768271446228027 seconds ---
--- 0.871166467666626 seconds ---
--- 1.197450876235962 seconds ---
--- 1.0472917556762695 seconds ---
--- 0.7846548557281494 seconds ---
--- 1.9216835498809814 seconds ---
--- 1.0944595336914062 seconds ---
--- 0.8136248588562012 seconds ---
--- 1.3200767040252686 seconds ---
--- 0.7287900447845459 seconds ---
--- 0.9563500881195068 seconds ---
--- 1.3003795146942139 seconds ---
--- 0.9947280883789062 seconds ---
--- 0.5776588916778564 seconds ---
--- 1.0171787738800049 seconds ---
--- 1.142371416091919 seconds ---
--- 1.5601370334625244 seconds ---
--- 0.9386310577392578 seconds ---
--- 1.080251932144165 seconds ---
--- 0.9790444374084473 seconds ---
--- 1.3279383182525635 seconds ---
--- 1.2630598545074463 seconds ---
--- 0.7337601184844971 seconds ---
Mean elapsed time: 1.317882627248764
A mean time of 1.3 seconds! With the D0 fast model, I don't really know if it is normal at all... With YOLOv3, I get much more than 30 FPS with the V100 with allegedly more FLOPS to compute... In case you manage to figure out what's going on (or if you simply can measure your elapsed time with the code above, please let me know).
Thank you for your cooperation! Vicent
Yeah, that's interesting, my inference time is much better than that with a GTX 1080Ti and efficientdet-d0:
...
--- 0.1885082721710205 seconds ---
--- 0.19663405418395996 seconds ---
--- 0.19111132621765137 seconds ---
--- 0.19989824295043945 seconds ---
--- 0.16353273391723633 seconds ---
--- 0.20000886917114258 seconds ---
--- 0.1951897144317627 seconds ---
--- 0.19105291366577148 seconds ---
--- 0.19177007675170898 seconds ---
--- 0.1879422664642334 seconds ---
--- 0.1805419921875 seconds ---
--- 0.1910994052886963 seconds ---
--- 0.1898207664489746 seconds ---
--- 0.19100117683410645 seconds ---
--- 0.19227385520935059 seconds ---
--- 0.1859912872314453 seconds ---
--- 0.19271111488342285 seconds ---
--- 0.19117498397827148 seconds ---
--- 0.19443821907043457 seconds ---
--- 0.18839311599731445 seconds ---
--- 0.1938471794128418 seconds ---
--- 0.18942928314208984 seconds ---
--- 0.19036245346069336 seconds ---
--- 0.19804883003234863 seconds ---
--- 0.18880176544189453 seconds ---
--- 0.1908419132232666 seconds ---
--- 0.19813323020935059 seconds ---
--- 0.19056463241577148 seconds ---
--- 0.18266630172729492 seconds ---
--- 0.1780529022216797 seconds ---
--- 0.19483685493469238 seconds ---
--- 0.19154143333435059 seconds ---
--- 0.16227436065673828 seconds ---
--- 0.19476962089538574 seconds ---
--- 0.15855860710144043 seconds ---
--- 0.1889948844909668 seconds ---
--- 0.19304347038269043 seconds ---
--- 0.16573548316955566 seconds ---
--- 0.1931142807006836 seconds ---
--- 0.19726085662841797 seconds ---
--- 0.19025206565856934 seconds ---
--- 0.16007256507873535 seconds ---
Mean elapsed time: 0.18534339486622933
I would check whether your GPU is actually used during the inference.
Yeah, that's interesting, my inference time is much better than that with a GTX 1080Ti and efficientdet-d0:
... --- 0.1885082721710205 seconds --- --- 0.19663405418395996 seconds --- --- 0.19111132621765137 seconds --- --- 0.19989824295043945 seconds --- --- 0.16353273391723633 seconds --- --- 0.20000886917114258 seconds --- --- 0.1951897144317627 seconds --- --- 0.19105291366577148 seconds --- --- 0.19177007675170898 seconds --- --- 0.1879422664642334 seconds --- --- 0.1805419921875 seconds --- --- 0.1910994052886963 seconds --- --- 0.1898207664489746 seconds --- --- 0.19100117683410645 seconds --- --- 0.19227385520935059 seconds --- --- 0.1859912872314453 seconds --- --- 0.19271111488342285 seconds --- --- 0.19117498397827148 seconds --- --- 0.19443821907043457 seconds --- --- 0.18839311599731445 seconds --- --- 0.1938471794128418 seconds --- --- 0.18942928314208984 seconds --- --- 0.19036245346069336 seconds --- --- 0.19804883003234863 seconds --- --- 0.18880176544189453 seconds --- --- 0.1908419132232666 seconds --- --- 0.19813323020935059 seconds --- --- 0.19056463241577148 seconds --- --- 0.18266630172729492 seconds --- --- 0.1780529022216797 seconds --- --- 0.19483685493469238 seconds --- --- 0.19154143333435059 seconds --- --- 0.16227436065673828 seconds --- --- 0.19476962089538574 seconds --- --- 0.15855860710144043 seconds --- --- 0.1889948844909668 seconds --- --- 0.19304347038269043 seconds --- --- 0.16573548316955566 seconds --- --- 0.1931142807006836 seconds --- --- 0.19726085662841797 seconds --- --- 0.19025206565856934 seconds --- --- 0.16007256507873535 seconds --- Mean elapsed time: 0.18534339486622933I would check whether your GPU is actually used during the inference.
That's quite strange... I tried the D0 and D7 models, and the average pass time is about the same, so I'm pretty sure that the time is spent in meaningless calculations...
Interesting indeed.
Yeah, the previous postprocessing runs on CPU with python, so it is slow.
Could you sync to the latest code (after https://github.com/google/automl/commit/08a55eddf051e82e9d5201f20685dde985a84af3) and try again? I have tested that saved_model_benchmark shows end-to-end latency is about 23ms/image on Colab K80 GPU.
Good morning,
I have tested it with the code above and I found a great improvement (with D0, it takes 18.9 ms ONLY FOR INFERENCE (not end-to-end latency)), but it still seems not as good as it might be...
Maybe you should measure the time of the inference itself and exclude the visualization and image saving, if that's not important for you. Also, you could try for a bigger batch size and modify the enumeration accordingly.
This only measures the inference:
# run inference on each image, visualize and save output for i, input in enumerate(imgs): start_time = time.time() predictions = driver.serve_images([input]) elapsed_time = time.time() - start_time img = driver.visualize(input, predictions[0], line_thickness=1) output_image_path = os.path.join(output_dir, str(i) + '.jpg') Image.fromarray(img).save(output_image_path) elapsed_list.append(elapsed_time) print("--- %s seconds ---" % elapsed_time) print("Mean elapsed time:", sum(elapsed_list)/len(elapsed_list))
thank ufor your code,i run it with 100 images, but it stop in the 14th image,have you ever see this error?
self._image -= offset File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py", line 899, in binary_op_wrapper return func(x, y, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/gen_math_ops.py", line 11086, in sub "Sub", x=x, y=y, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/op_def_library.py", line 794, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 3357, in create_op attrs, op_def, compute_device) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 3426, in _create_op_internal op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 1748, in init self._traceback = tf_stack.extract_stack()
Maybe you should measure the time of the inference itself and exclude the visualization and image saving, if that's not important for you. Also, you could try for a bigger batch size and modify the enumeration accordingly.
This only measures the inference:
# run inference on each image, visualize and save output for i, input in enumerate(imgs): start_time = time.time() predictions = driver.serve_images([input]) elapsed_time = time.time() - start_time img = driver.visualize(input, predictions[0], line_thickness=1) output_image_path = os.path.join(output_dir, str(i) + '.jpg') Image.fromarray(img).save(output_image_path) elapsed_list.append(elapsed_time) print("--- %s seconds ---" % elapsed_time) print("Mean elapsed time:", sum(elapsed_list)/len(elapsed_list))
when i do that code, i got some error after 13th images:
start image 14
self._image -= offset
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py", line 899, in binary_op_wrapper return func(x, y, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/gen_math_ops.py", line 11086, in sub "Sub", x=x, y=y, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/op_def_library.py", line 794, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 3357, in create_op attrs, op_def, compute_device) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 3426, in _create_op_internal op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 1748, in init self._traceback = tf_stack.extract_stack()
Nope, I never had this issue.