'Do inference used tensorflow-gpu1.15 with a trained model, Get "ValueError: underlying buffer has been detached"
I haved trained a segmentation model, and then i would like to use this trained model to do inference at some test images. the following code is used.
import io
import os
import sys
import numpy as np
import tensorflow as tf
from PIL import Image
from PIL import ImageFile
from utils import load_config
ImageFile.LOAD_TRUNCATED_IMAGES = True
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding='utf-8')
def resize_image(image, new_size, resize_method):
if resize_method == 'square':
return image.resize((new_size, new_size), Image.ANTIALIAS)
elif resize_method == 'inscale':
orig_width, orig_height = image.size
orig_max_size = orig_width if (orig_height < orig_width) else orig_height
# resize
scale_factor = new_size / orig_max_size
new_width = int(orig_width * scale_factor)
new_height = int(orig_height * scale_factor)
image = image.resize([new_width, new_height], Image.ANTIALIAS)
return image
def pad(image, new_size):
new_height, new_width, _ = image.shape
h_need_pad = new_size - new_height
h_top = int(h_need_pad / 2)
h_bottom = h_need_pad - h_top
w_need_pad = new_size - new_width
w_left = int(w_need_pad / 2)
w_right = w_need_pad - w_left
pad_image = np.pad(image,
((h_top, h_bottom), (w_left, w_right), (0, 0)),
mode='constant',
constant_values=0)
pad_image = pad_image.astype(np.float32)
return pad_image
def preprocess_image(image_path, config):
origin_ref_image = Image.open(image_path, 'r').convert('RGB')
w, h = origin_ref_image.size
image = resize_image(origin_ref_image, config.train_size , config.resize_method)
image = np.array(image, dtype=np.float32)
if config.resize_method == 'inscale':
image = pad(image, config.train_size)
image = (image - config.mean_rgb) / config.var
image = np.expand_dims(image, axis=0)
return image, w, h
def crop_image(image, new_size, orig_width, orig_height):
orig_max_size = orig_width if (orig_height < orig_width) else orig_height
scale_factor = new_size / orig_max_size
new_width = int(orig_width * scale_factor)
new_height = int(orig_height * scale_factor)
h_need_pad = new_size - new_height
h_top = int(h_need_pad / 2)
h_bottom = h_top + new_height
w_need_pad = new_size - new_width
w_left = int(w_need_pad / 2)
w_right = w_left + new_width
return image[h_top:h_bottom, w_left:w_right]
def main_fast(image_folder, pb_file_path, config, score_folder, with_softmax):
graph_def = tf.GraphDef()
with open(pb_file_path, "rb") as f:
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name="")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
#input = sess.graph.get_tensor_by_name("input:0")
#raw_output = sess.graph.get_tensor_by_name("softmax/Reshape_1:0")
input = sess.graph.get_tensor_by_name("input_node:0")
raw_output = sess.graph.get_tensor_by_name("output_node:0")
if not with_softmax:
raw_output = tf.contrib.slim.softmax(raw_output)
background_score, foreground_score = tf.split(raw_output, num_or_size_splits=2, axis=3)
foreground_score = tf.squeeze(foreground_score)
images_names = os.listdir(os.path.join(image_folder))
images_names.sort()
# result directory
if not os.path.exists(score_folder):
os.makedirs(score_folder)
for i in range(0, len(images_names)):
if i%100==0:
print(f'Start process image: {i}')
cur_frame_path = os.path.join(image_folder, images_names[i])
cur_image, origin_width, origin_height = preprocess_image(cur_frame_path, config)
fore_score = sess.run(foreground_score, feed_dict={input: cur_image})
# process prediction
fore_score = fore_score * 255
fore_score = np.array(fore_score, dtype=np.uint8)
fore_score = crop_image(fore_score, config.train_size, origin_width, origin_height)
score_image = Image.fromarray(fore_score, mode='L')
score_image = score_image.resize([origin_width, origin_height], resample=Image.BILINEAR)
save_path = os.path.join(score_folder, images_names[i][:-4] + '-mask.png')
score_image.save(save_path)
if __name__ == '__main__':
config = load_config.load_yaml(yaml_name='config_files/apu_384.yml', quantize=False)
pb_file_path = 'xxx.pb'
with_softmax = False
image_folder= '../images'
save_dir = 'results'
main_fast(image_folder, pb_file_path, config, save_dir, with_softmax)
but i meet following error, bellowing is error info.I'm confused, because i have trained model successfully. but error occured when do inference.
2022-04-07 08:22:39.354461: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3699850000 Hz
2022-04-07 08:22:39.354980: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7837e80 initialized for p
latform Host (this does not guarantee that XLA will be used). Devices:
2022-04-07 08:22:39.354992: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
--- Logging error ---
Traceback (most recent call last):
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/tensorflow_core/python/util/module_wrapper.py", line
167, in __getattribute__
return attr_map[name]
KeyError: 'GraphDef'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/.pyenv/versions/3.6.8/lib/python3.6/logging/__init__.py", line 996, in emit
stream.write(msg)
ValueError: underlying buffer has been detached
Call stack:
File "test.py", line 132, in <module>
main_fast(image_folder, pb_file_path, config, save_dir, with_softmax)
File "test.py", line 83, in main_fast
graph_def = tf.GraphDef()
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/tensorflow_core/python/util/module_wrapper.py", line
187, in __getattribute__
self._tfmw_add_deprecation_warning(name, attr)):
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/tensorflow_core/python/util/module_wrapper.py", line
139, in _tfmw_add_deprecation_warning
_call_location(), full_name, rename)
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/tensorflow_core/python/platform/tf_logging.py", line
get_logger().warning(msg, *args, **kwargs)
Message: 'From %s: The name %s is deprecated. Please use %s instead.\n'
Arguments: ('test.py:83', 'tf.GraphDef', 'tf.compat.v1.GraphDef')
--- Logging error ---
Traceback (most recent call last):
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/tensorflow_core/python/util/module_wrapper.py", line
return attr_map[name]
KeyError: 'Session'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/.pyenv/versions/3.6.8/lib/python3.6/logging/__init__.py", line 996, in emit
stream.write(msg)
ValueError: underlying buffer has been detached
Call stack:
File "test.py", line 132, in <module>
main_fast(image_folder, pb_file_path, config, save_dir, with_softmax)
File "test.py", line 88, in main_fast
with tf.Session() as sess:
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/tensorflow_core/python/util/module_wrapper.py", line
self._tfmw_add_deprecation_warning(name, attr)):
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/tensorflow_core/python/util/module_wrapper.py", line
_call_location(), full_name, rename)
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/tensorflow_core/python/platform/tf_logging.py", line
get_logger().warning(msg, *args, **kwargs)
Message: 'From %s: The name %s is deprecated. Please use %s instead.\n'
Arguments: ('test.py:88', 'tf.Session', 'tf.compat.v1.Session')
start process image:1
following is all installed pakages in my python environments
root@1aa0f8999969:/portrait_segmentation1# pip list
Package Version
-------------------- -----------
absl-py 0.9.0
astor 0.8.1
astroid 2.4.1
attrs 19.3.0
backcall 0.1.0
bleach 3.1.5
contextlib2 0.6.0.post1
cycler 0.10.0
Cython 0.29.17
decorator 4.4.2
defusedxml 0.6.0
easydict 1.9
entrypoints 0.3
future 0.18.2
gast 0.2.2
google-pasta 0.2.0
grpcio 1.29.0
h5py 2.10.0
importlib-metadata 1.6.0
ipykernel 5.2.1
ipython 7.14.0
ipython-genutils 0.2.0
ipywidgets 7.5.1
isort 4.3.21
jedi 0.17.0
Jinja2 2.11.2
joblib 0.15.1
jsonschema 3.2.0
jupyter 1.0.0
jupyter-client 6.1.3
jupyter-console 6.1.0
jupyter-core 4.6.3
Keras-Applications 1.0.8
Keras-Preprocessing 1.1.2
kiwisolver 1.2.0
lazy-object-proxy 1.4.3
lxml 4.2.4
Markdown 3.2.2
MarkupSafe 1.1.1
matplotlib 3.2.1
mccabe 0.6.1
mistune 0.8.4
mock 4.0.2
nbconvert 5.6.1
nbformat 5.0.6
nest 1.3.0
nets 0.0.2
notebook 6.0.3
numpy 1.18.4
opencv-python 3.4.6.27
opt-einsum 3.2.1
packaging 20.3
pandas 1.0.3
pandocfilters 1.4.2
parso 0.7.0
pexpect 4.8.0
pickleshare 0.7.5
Pillow 7.1.2
pip 20.1.1
prometheus-client 0.7.1
prompt-toolkit 3.0.5
protobuf 3.12.0
ptyprocess 0.6.0
pycocotools-fix 2.0.0.1
Pygments 2.6.1
pylint 2.5.2
pyparsing 2.4.7
PyQt5 5.10.1
pyrsistent 0.16.0
python-dateutil 2.8.1
pytz 2020.1
PyYAML 6.0
pyzmq 19.0.1
qtconsole 4.7.4
QtPy 1.9.0
scikit-learn 0.23.1
scipy 1.4.1
Send2Trash 1.5.0
setuptools 46.4.0
sip 4.19.8
six 1.14.0
sklearn 0.0
tensorboard 1.15.0
tensorflow-estimator 1.15.1
tensorflow-gpu 1.15.0rc3
termcolor 1.1.0
terminado 0.8.3
testpath 0.4.4
tf-slim 1.1.0
threadpoolctl 2.0.0
toml 0.10.1
torch 1.5.0
tornado 6.0.4
traitlets 4.3.3
typed-ast 1.4.1
utils 1.0.1
wcwidth 0.1.9
webencodings 0.5.1
Werkzeug 1.0.1
wheel 0.34.2
widgetsnbextension 3.5.1
wrapt 1.12.1
zipp 3.1.0
I wonder where the problem is, hope anyone can help me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
