繁体   English   中英

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

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

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

ValueError:在用户代码中:

/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)

这是提取人脸的代码:

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()

这是匹配面的代码:

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")

老问题,但将extract_face_from_image更改为:

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

工作得很好。

暂无
暂无

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

相关问题 ValueError: Input 0 is incompatible with layer similar_model: expected shape=(None, 224, 224, 3), found shape=(None, None, 224, 224, 3) Tensorflow / Keras ValueError:层“模型”的输入 0 与层不兼容:预期形状=(无,224,224,3),发现形状=(32,224,3) ValueError: 图层“model_1”的输入 0 与图层不兼容:预期形状=(None, 224, 224, 3),发现形状=(None, 290, 290, 3) ValueError:层“model_10”的输入 0 与层不兼容:预期形状 =(None, 244, 244, 3),找到的形状 =(None, 224, 224, 3) 输入 0 与层 functional_3 不兼容:预期 shape=(None, 224, 224, 3),发现 shape=(None, 240, 240, 3) ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3 [None, 224, 224, 1] ValueError:层 sequential_16 的输入 0 与层不兼容:预期 ndim=5,发现 ndim=4。 收到的完整形状:[无,224、224、3] 我收到错误,例如“图层“model_5”的输入 1 与图层不兼容:预期形状 =(无,224、224、3),找到形状 =(无,5) ValueError:层 sequential_3 的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 已收到完整形状:(无、224、256) ValueError:检查输入时出错:预期input_2具有形状(224,224,3)但得到形状为(224,224,4)的数组
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM