简体   繁体   English

为什么我在运行此脚本时收到 tensorflow 警告?

[英]why am i getting a tensorflow warning when running this script?

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)

the code does the job by detecting and extracting the faces from the input folder and putting the extracted faces in the output folder without any issues but i wanna know why is this warning is showing up in the first place and is there any way i can fix it "properly" instead of suppressing it该代码通过从输入文件夹中检测和提取人脸并将提取的人脸放入 output 文件夹中来完成这项工作,没有任何问题,但我想知道为什么这个警告首先出现,有什么办法可以解决它“正确地”而不是压制它

details细节

  • TensorFlow version (CPU):2.7.0 TensorFlow版本(CPU):2.7.0

  • python version 3.8.4 python 版本 3.8.4

the warning message is WARNING:tensorflow:5 out of the last 9 calls to <function Model.make_predict_function..predict_function at 0x0000000013E161F0> triggered tf.function retracing. the warning message is WARNING:tensorflow:5 out of the last 9 calls to <function Model.make_predict_function..predict_function at 0x0000000013E161F0> 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.跟踪是昂贵的,过多的跟踪可能是由于(1)在循环中重复创建@tf.function,(2)传递不同形状的张量,(3)传递 Python 对象而不是张量。 For (1), please define your @tf.function outside of the loop.对于 (1),请在循环外定义您的 @tf.function。 For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing.对于 (2),@tf.function 具有 Experimental_relax_shapes=True 选项,可以放宽可以避免不必要的回溯的参数形状。 For (3), please refer tohttps://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details. For (3), please refer tohttps://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.

In your case warning happens because of (2) passing tensors with different shapes .在您的情况下,由于(2) passing tensors with different shapes发生警告。 And you get tensors of different shape because of PIL.由于 PIL,你会得到不同形状的张量。 img.thumbnail resizes some of your images in a wrong way. img.thumbnail以错误的方式调整某些图像的大小。 It keeps the aspect ratio of the image, so you get 160x160 image only if your source image had 1:1 aspect ratio.它保持图像的纵横比,因此只有当您的源图像具有 1:1 的纵横比时,您才能获得 160x160 的图像。

Use Tensorflow or OpenCV to process images, not PIL it's very slow...使用 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, (160, 160))
    results = detector.detect_faces(img)
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM