Executando label_image.py em um loop
Meu objetivo é classificar continuamente imagens .jpg provenientes de um fluxo de vídeo.
Para isso, acabei de modificar oexemplo label_image.py.
Estou carregando o gráfico e abrindo as sessões com antecedência. Então, eu estou executando apenas o seguinte código em um loop:
t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
results = sess2.run(output_operation.outputs[0],
{input_operation.outputs[0]: t}
)
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
labels = load_labels(label_file)
Funciona bem por alguns minutos, mas o problema é que a cada ciclo a classificação diminui progressivamente. Vai de meio segundo a alguns segundos em um minuto. Meu uso de memória também está aumentando lentamente, um aumento de cerca de 1 MB a cada 3 segundos.
Se eu classificar uma única imagem várias vezes, deixando de fora o "read_tensor_from_image_file", não recebo esse bug.
Portanto, algo no código de carregamento da imagem deve estar ocupando mais espaço todas as vezes, não limpando adequadamente:
def read_tensor_from_image_file(file_name, input_height=192, input_width=192,
input_mean=0, input_std=255):
input_name = "file_reader"
output_name = "normalized"
file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels = 3,
name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
result = sess1.run(normalized)
return result
Todas as sugestões são muito apreciadas, estou totalmente presa a essa.
Estou usando python 3.4.2 com tensorflow 1.1.0 em um raspberry pi com raspbian jessie.
Muito obrigado !