【问题标题】:why am i getting a tensorflow warning when running this script?为什么在运行此脚本时会收到 tensorflow 警告?
【发布时间】:2022-01-15 01:41:31
【问题描述】:
import glob
import os
from mtcnn.mtcnn import MTCNN
import warnings
import time

from numpy import asarray
from PIL import Image
#warnings.filterwarnings("ignore")
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

directory = input("insert input path \n")

output_directory = input("insert output path \n")
#mode=input("do you want to conver the outputs to Grayscale ?")
img_names=glob.glob(os.path.join(directory+"/*.jpg"))

detector = MTCNN()
def process_image(img_name,mode='L',output_size=(160,160)):
    img = Image.open(directory+img_name)
    img.thumbnail((160,160))
    pixels=asarray(img)
    results = detector.detect_faces(pixels)
    if results:
        # extract the bounding box from the requested face
        x1 ,y1,width,height=results[0]['box']
        x1,y1=abs(x1),abs(y1)
        x2,y2=x1 + width,y1 + height
        # extract the face by slicing
        face_boundary = pixels[y1:y2, x1:x2]
        # resize pixels to the model size
        #image1 = Image.fromarray(face_boundary)
        #image1 = image.resize(required_size)
        image=Image.fromarray(face_boundary)
        #if mode=='L':
         #   image=image.convert('L')
        image = image.resize(output_size)
        #image.thumbnail((160,160))
        #image = image.resize(())
        #face_array = asarray(image)
    #image.save(f"/kaggle/input/rashaa/rasha{img_name}")
        image.save(f'{output_directory}{img_name}')     
        print(f'{img_name} was processed...')
#for img in img_names:
 #       x.append(img.replace(directory,""))
x=[img.replace(directory,"") for img in img_names]
t1 = time.perf_counter()
y=[process_image(img) for img in x]

t2=time.perf_counter()
print(t2-t1)

代码通过检测和提取输入文件夹中的人脸并将提取的人脸放入输出文件夹来完成这项工作,没有任何问题 但我想知道为什么这个警告首先出现,有什么办法可以“正确”修复它而不是抑制它

详情

  • TensorFlow 版本(CPU):2.7.0

  • python 版本 3.8.4

警告消息是 WARNING:tensorflow:5 out of the last 9 calls to 触发了 tf.function 回溯。追踪 是昂贵的,过多的跟踪可能是由于(1)在循环中重复创建 @tf.function,(2)传递不同形状的张量,(3)传递 Python 对象而不是张量。对于 (1),请在循环之外定义您的 @tf.function。对于 (2),@tf.function 有 Experimental_relax_shapes=True 选项,可以放宽参数 可以避免不必要的回溯的形状。 (3) 详情请参考https://www.tensorflow.org/guide/function#controlling_retracinghttps://www.tensorflow.org/api_docs/python/tf/function

【问题讨论】:

  • 您使用的是哪个 TensorFlow 版本?我在 Tensorflow 2.4.1 上尝试了您的代码 sn-p,但没有收到该警告。
  • 我现在用的是TensorFlow 2.7.0,你用的是GPU还是CPU版本?
  • 我也尝试了 2.7.0 以及 CPU 和 GPU 版本。仍然没有错误消息。

标签: python tensorflow computer-vision warnings face-detection


【解决方案1】:

在您的情况下,由于(2) passing tensors with different shapes 而发生警告。由于 PIL,你会得到不同形状的张量。 img.thumbnail 以错误的方式调整某些图像的大小。它会保持图像的纵横比,因此只有当您的源图像具有 1:1 的纵横比时,您才能获得 160x160 的图像。

使用 Tensorflow 或 OpenCV 处理图像,而不是 PIL,它很慢...

def process_image(img_name, output_size=(160,160)):
    img = cv2.imread(directory+img_name)
    img = img[..., ::-1] # convert BGR to RGB
    img = cv2.resize(img, output_size)
    results = detector.detect_faces(img)
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多