【问题标题】:Warnings in TensorFlow while using MTCNN使用 MTCNN 时 TensorFlow 中的警告
【发布时间】:2020-08-17 13:15:31
【问题描述】:

我正在做一个检测人脸的项目,我正在使用以下代码:

# demonstrate face detection on 5 Celebrity Faces Dataset
from os import listdir
from PIL import Image
from numpy import asarray
from matplotlib import pyplot
from mtcnn.mtcnn import MTCNN

# extract a single face from a given photograph
def extract_face(filename, required_size=(160, 160)):
    # load image from file
    image = Image.open(filename)
    # convert to RGB, if needed
    image = image.convert('RGB')
    # convert to array
    pixels = asarray(image)
    # create the detector, using default weights
    detector = MTCNN()
    # detect faces in the image
    results = detector.detect_faces(pixels)
    # extract the bounding box from the first face
    x1, y1, width, height = results[0]['box']
    # bug fix
    x1, y1 = abs(x1), abs(y1)
    x2, y2 = x1 + width, y1 + height
    # extract the face
    face = pixels[y1:y2, x1:x2]
    # resize pixels to the model size
    image = Image.fromarray(face)
    image = image.resize(required_size)
    face_array = asarray(image)
    return face_array

# specify folder to plot
#folder = '5-celebrity-faces-dataset/train/ben_afflek/'
folder = '/content/drive/My Drive/ufc-project/5-celebrity-faces-dataset/train/ben_afflek'
i = 1
# enumerate files
for filename in listdir(folder):
    # path
    path = folder + '/' + filename
    # get face
    face = extract_face(path)
    print(i, face.shape)
    # plot
    pyplot.subplot(2, 7, i)
    pyplot.axis('off')
    pyplot.imshow(face)
    i += 1
pyplot.show()

我从本教程中得到了这段代码:https://machinelearningmastery.com/how-to-develop-a-face-recognition-system-using-facenet-in-keras-and-an-svm-classifier/?unapproved=549711&moderation-hash=6b355b586de2f2ff191df14529849990#comment-549711

它正在工作,但我收到以下警告:

WARNING:tensorflow:6 out of the last 11 calls to <function Model.make_predict_function..predict_function at 0x7f0016e2eae8> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.

有人知道怎么解决吗?

【问题讨论】:

    标签: tensorflow deep-learning computer-vision conv-neural-network


    【解决方案1】:

    每次调用提取人脸时,您都在创建检测器。将人脸检测器创建移出循环并将其作为参数传递给函数。 将 linedetector = MTCNN() 移到 extract_face 之外,特别是在循环之前。

    【讨论】:

      猜你喜欢
      • 2018-05-26
      • 2021-01-31
      • 1970-01-01
      • 2021-12-27
      • 2017-07-16
      • 2019-07-08
      • 2019-05-27
      • 2020-07-22
      • 1970-01-01
      相关资源
      最近更新 更多