AttributeError: โมดูล 'tensorflow' ไม่มีแอตทริบิวต์ 'get_default_graph' ใน tensorflow

Aug 18 2020

ฉันได้รับข้อผิดพลาดต่อไปนี้ในรหัสของฉัน

import mtcnn
# print version
print(mtcnn.__version__)

# 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
print("MTCNN: {}".format(mtcnn.__version__))
import tensorflow as tf
from tensorflow import keras

# 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 = '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()

ข้อผิดพลาด:

anaconda3 \ envs \ py3 \ lib \ site-Packages \ keras \ backend \ tensorflow_backend.py "บรรทัดที่ 68 ในกราฟ get_uid = tf.get_default_graph ()

AttributeError: โมดูล 'tensorflow' ไม่มีแอตทริบิวต์ 'get_default_graph'

ฉันลองนำเข้าหลายรายการ แต่ไม่ได้ผล ดูเหมือนว่าข้อผิดพลาดนี้จะเป็นปกติ แต่ฉันไม่พบสิ่งใดที่ช่วยแก้ปัญหาของฉันได้

คำตอบ

1 TimbusCalin Aug 18 2020 at 13:32

ในคำตอบที่นี่นำเข้า keras จากเทนเซอร์โฟลว์เช่นเดียวกับที่คุณแก้ปัญหา

แต่ปัญหาในกรณีของคุณคือ MTCNN ทำงานบน Keras บริสุทธิ์แทนที่จะเป็น TensorFlow ดังนั้นการที่คุณโหลดใน Keras "main.py" จาก tensorflow จึงไม่มีผลใด ๆ คุณจำเป็นต้องดาวน์เกรดเวอร์ชันเทนเซอร์โฟลว์หรือคุณแก้ไขการนำเข้าทั้งหมดใน MTCNN ซึ่งไม่รับประกันว่าจะทำงานได้อย่างน่าเสียดาย