首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ValueError:输入0与层vggface_resnet50不兼容:预期的shape=(None,224,224,3),发现的shape=(None,1,224,224,3)

ValueError:输入0与层vggface_resnet50不兼容:预期的shape=(None,224,224,3),发现的shape=(None,1,224,224,3)
EN

Stack Overflow用户
提问于 2020-12-20 05:42:48
回答 1查看 630关注 0票数 2

我想使用https://morioh.com/p/a07857cbc76d中的代码来匹配我的人脸项目的chokepoint数据集中的人脸,我提取了人脸,并希望使用VGGface和余弦来匹配它们,但得到了这个错误,你能帮助我吗?

ValueError:在用户代码中:

代码语言:javascript
运行
复制
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1478 predict_function  *
    return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1468 step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
    return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1461 run_step  **
    outputs = model.predict_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1434 predict_step
    return self(x, training=False)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
    input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:274 assert_input_compatibility
    ', found shape=' + display_shape(x.shape))

ValueError: Input 0 is incompatible with layer vggface_resnet50: expected shape=(None, 224, 224, 3), found shape=(None, 1, 224, 224, 3)

下面是提取人脸的代码:

代码语言:javascript
运行
复制
def extract_face_from_image(image_path, required_size=(224, 224)):
    image = plt.imread(image_path)
    detector = MTCNN()
    faces = detector.detect_faces(image)
    face_images = []
    for face in faces:
        x1, y1, width, height = face['box']
        x2, y2 = x1 + width, y1 + height
        face_boundary = image[y1:y2, x1:x2]
        face_image = Image.fromarray(face_boundary)
        face_image = face_image.resize(required_size)
        face_array = asarray(face_image)
        face_images.append(face_array)
    return face_images
extracted_face = extract_face_from_image('/content/301.jpg')
plt.imshow(extracted_face[0])
plt.show()

这是匹配人脸的代码:

代码语言:javascript
运行
复制
def get_model_scores(faces):
    samples = asarray(faces, 'float32')
    samples = preprocess_input(samples, version=2)
    model = VGGFace(model='resnet50', include_top=False, input_shape=(224, 224, 3), pooling='avg')
    return model.predict(samples)
faces = [extract_face_from_image(image_path) for image_path in ['/content/125.jpg', 
'/content/126.jpg']]
model_scores = get_model_scores(faces)
if cosine(model_scores[0], model_scores[1]) <= 0.4:
    print("Faces Matched")
EN

回答 1

Stack Overflow用户

发布于 2021-07-05 17:26:31

老问题,但将extract_face_from_image更改为:

代码语言:javascript
运行
复制
def extract_face_from_image(image_path, required_size=(224, 224)):
    # load image and detect faces
    image = plt.imread(image_path)

    detector = MTCNN()
    faces = detector.detect_faces(image)

    # extract the bounding box from the requested face
    x1, y1, width, height = faces[0]['box']
    x2, y2 = x1 + width, y1 + height

    # extract the face
    face_boundary = image[y1:y2, x1:x2]

    image = cv2.resize(face_boundary, required_size)

    return image

工作得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65374755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档