Я использую код Python (v 3.6.5), который использует TensorFlow (v 1.13.2) для выполнения вывода с использованием обученной модели (в Windows 8.1).
Я хочудля перехвата (и регистрации) исключений / ошибок, которые генерируются из библиотеки TensorFlow.
Например, когда размер пакета (во время session.run ()) слишком велик, процесс использует всю системную памятьи сбой.
Мой код выглядит следующим образом:
import tensorflow as tf
import math
from tqdm import tqdm
# …
def parse_function(image_string, frame_id):
image = tf.image.decode_jpeg(image_string, channels=3)
resize_image = tf.image.resize_images(image, [224, 224], method=tf.image.ResizeMethod.BICUBIC)
return resize_image, frame_id
def load_graph(frozen_graph_filename):
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name="prefix")
return graph
def main(_):
batch_size = 128
num_frames = 5000
num_batches = int(np.ceil(num_frames / batch_size))
frame_ids = get_ids()
with MyFrameReader() as frd:
im_list = []
for id in frame_ids:
im_list.append(frd.get_frame(id))
dataset = tf.data.Dataset.from_tensor_slices((im_list, frame_ids))
dataset = dataset.map(parse_function)
batched_dataset = dataset.batch(batch_size)
iterator = batched_dataset.make_initializable_iterator()
next_element = iterator.get_next()
graph = load_graph(PB_FILE)
x = graph.get_tensor_by_name('prefix/input_image:0')
y = graph.get_tensor_by_name('prefix/output_node:0')
sess1 = tf.Session(graph=graph)
sess2 = tf.Session(config= tf.ConfigProto(device_count={'GPU': 0})) # Run on CPU
sess2.run(iterator.initializer)
for _ in tqdm(range(num_batches)):
try:
# pre process
inference_batch, frame_id_batch = sess2.run(next_element)
# main process
scores_np = sess1.run(y, feed_dict={x: inference_batch})
# post process …
except MemoryError as e:
print('Error 1')
except Exception as e:
print('Error 2')
except tf.errors.OpError as e:
print('Error 3')
except:
print('Error 4')
sess1.close()
sess2.close()
Я вижу, что память процесса растет и в какой-то момент он умирает, не достигнув кода обработки исключений,(если я добавляю в python код, который улавливает память, мне удается поймать исключение памяти)
Может кто-нибудь объяснить, что происходит?