使用图像识别时增加内存消耗

2024-05-14 04:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用tensorflow的图像识别从多个图像中获取标签,基于以下示例: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/imagenet/classify_image.py

我遇到的问题是,每次调用模块时,进程都会增加其内存,而且它永远不会被释放,表现得就像内存泄漏一样。在

for sPhotoPath in lsPhotoFiles:
    print('Process memory: '+str(memory() / 1e6)+' MB')
    print('Recognizing image: '+sPhotoPath)
    ldTags = imagerecognition.getImageTags(sPhotoPath)
    print(ldTags)

该模块看起来像:

^{pr2}$

只修改了部分代码:

def run_inference_on_image(image):
  """Runs inference on an image.

  Args:
    image: Image file name.

  Returns:
    Nothing
  """
  if not tf.gfile.Exists(image):
    tf.logging.fatal('File does not exist %s', image)
  image_data = tf.gfile.FastGFile(image, 'rb').read()

  # Creates graph from saved GraphDef.
  create_graph()

  with tf.Session() as sess:
    # Some useful tensors:
    # 'softmax:0': A tensor containing the normalized prediction across
    #   1000 labels.
    # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
    #   float description of the image.
    # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
    #   encoding of the image.
    # Runs the softmax tensor by feeding the image_data as input to the graph.
    softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
    predictions = sess.run(softmax_tensor,
                           {'DecodeJpeg/contents:0': image_data})
    predictions = np.squeeze(predictions)

    # Creates node ID --> English string lookup.
    node_lookup = NodeLookup()

    top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
    lsTags = []
    for node_id in top_k:
      human_string = node_lookup.id_to_string(node_id) # 'dock, dockage, docking facility'
      score = predictions[node_id]
      # print('%s (score = %.5f)' % (human_string, score))
      lsHumanString = map((lambda s: s.strip()), human_string.split(','))
      sTag = lsHumanString[0]
      lsTags.append(sTag)

  return lsTags

输出如下:


nil@Lenovo-Z50-70:~/miscellaneous/duplicate/image_recognition$ python test.py 
Process memory: 911.888384 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342502.jpg
>> Downloading inception-2015-12-05.tgz 100.0%
Succesfully downloaded inception-2015-12-05.tgz 88931400 bytes.
W tensorflow/core/framework/op_def_util.cc:332] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().
['chiffonier', 'wardrobe', 'file', 'studio couch', 'four-poster']
Process memory: 1750.99904 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/40118_479346.jpg
['restaurant', 'bookshop', 'tobacco shop', 'library', 'bakery']
Process memory: 1812.754432 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342503.jpg
['shoji', 'four-poster', 'window shade', 'sliding door', 'studio couch']
Process memory: 1916.932096 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/40118_479343.jpg
['studio couch', 'four-poster', 'quilt', 'wardrobe', 'mosquito net']
Process memory: 2024.63232 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342512.jpg
['space bar', 'typewriter keyboard', 'table lamp', 'lampshade', 'printer']
Process memory: 2131.304448 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/40118_479345.jpg
['shoji', 'four-poster', 'window shade', 'sliding door', 'studio couch']
Process memory: 2240.454656 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/40118_479349.jpg
['sliding door', 'wardrobe', 'entertainment center', 'medicine chest', 'shoji']
Process memory: 2347.286528 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342505.jpg
['wardrobe', 'shoji', 'window shade', 'studio couch', 'quilt']
Process memory: 4010.266624 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342511.jpg
['chiffonier', 'wardrobe', 'file', 'chest', 'crib']
Process memory: 4312.395776 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342508.jpg
['moving van', 'boathouse', 'sliding door', 'butcher shop', 'forklift']
Process memory: 4613.394432 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342498.jpg
['dock', 'lakeside', 'seashore', 'pier', 'liner']
Process memory: 4915.449856 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342509.jpg
['studio couch', 'dining table', 'four-poster', 'patio', 'china cabinet']
Process memory: 5217.521664 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/40118_479344.jpg
['dock', 'lakeside', 'seashore', 'container ship', 'liner']
Process memory: 5518.82752 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/40118_479347.jpg
['restaurant', 'tobacco shop', 'microwave', 'shoji', 'home theater']
Process memory: 5820.383232 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342510.jpg
['studio couch', 'crate', 'wardrobe', 'four-poster', 'chiffonier']
Process memory: 6122.430464 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342504.jpg
['restaurant', 'altar', 'tobacco shop', 'sewing machine', 'desk']
Process memory: 6423.30624 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/40118_479350.jpg
['studio couch', 'four-poster', 'dining table', 'wardrobe', 'entertainment center']
Process memory: 6725.758976 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/2823240_23342501.jpg
['studio couch', 'four-poster', 'quilt', 'wardrobe', 'mosquito net']
Process memory: 7026.778112 MB
Recognizing image: /home/nil/miscellaneous/duplicate/image_recognition/../image_matching/photos/group1031/airbnb/40118_479352.jpg
['wardrobe', 'shoji', 'window shade', 'studio couch', 'quilt']
Process memory: 7328.5632 MB
...

我检查了会话和图形都被^{pr 5}$包装

在主循环中的每次迭代之后,如何释放内存?谢谢


Tags: imagehomembprocessmiscellaneousjpgmemorynil