值错误:加载预先训练的模型facenet_keras.h5时,元帅数据不正确

时间:2020-09-15 06:18:50

标签: python tensorflow machine-learning keras facenet

这是我的代码,用于在加载预训练的模型facenet_keras.h5的地方准备数据 错误发生在我正在加载模型的第16行。

import os
import pickle
import numpy as np
import cv2
import mtcnn

from keras.models import load_model
from utils import get_face, get_encode, l2_normalizer, normalize
encoder_model = r'C:\Users\hussa\PycharmProjects\FCN\_03_facenet_keras\data\model\facenet_keras.h5'
people_dir = 'C:/Users/hussa/PycharmProjects/FCN/_03_facenet_keras/data/people'
encodings_path = 'C:/Users/hussa/PycharmProjects/FCN/_03_facenet_keras/data/encodings/encodings.pkl'

required_size = (160, 160)
face_detector = mtcnn.MTCNN()
face_encoder = load_model(encoder_model)
encoding_dict = dict()

for person_name in os.listdir(people_dir):
person_dir = os.path.join(people_dir, person_name)
encodes = []
for img_name in os.listdir(person_dir):
    img_path = os.path.join(person_dir, img_name)
    img = cv2.imread(img_path)
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = face_detector.detect_faces(img_rgb)
    if results:
        res = max(results, key=lambda b: b['box'][2] * b['box'][3])
        face, _, _ = get_face(img_rgb, res['box'])
        face = normalize(face)
        face = cv2.resize(face, required_size)
        encode = face_encoder.predict(np.expand_dims(face, axis=0))[0]
        encodes.append(encode)
if encodes:
    encode = np.sum(encodes, axis=0)
    encode = l2_normalizer.transform(np.expand_dims(encode, axis=0))[0]
    encoding_dict[person_name] = encode
for key in encoding_dict.keys():
    print(key)
with open(encodings_path, 'bw') as file:
    pickle.dump(encoding_dict, file)

错误消息显示:

"Traceback (most recent call last):

  File "C:/Users/hussa/PycharmProjects/FCN/_03_facenet_keras/01_prepare_data.py", line 16, in <module>
    face_encoder = load_model(encoder_model)

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\saving\save.py", line 182, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
 
 File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 177, in load_model_from_hdf5
    model = model_config_lib.model_from_config(model_config,

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\saving\model_config.py", line 55, in model_from_config
    return deserialize(config, custom_objects=custom_objects)

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\layers\serialization.py", line 171, in deserialize
    return generic_utils.deserialize_keras_object(

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 354, in deserialize_keras_object
    return cls.from_config(

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 2238, in from_config
    return functional.Functional.from_config(

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\functional.py", line 616, in from_config
    input_tensors, output_tensors, created_layers = reconstruct_from_config(

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\functional.py", line 1204, in reconstruct_from_config
    process_layer(layer_data)

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\functional.py", line 1186, in process_layer
    layer = deserialize_layer(layer_data, custom_objects=custom_objects)

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\layers\serialization.py", line 171, in deserialize
    return generic_utils.deserialize_keras_object(

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 354, in deserialize_keras_object
    return cls.from_config(

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\layers\core.py", line 1005, in from_config
    function = cls._parse_function_from_config(

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\layers\core.py", line 1057, in _parse_function_from_config
    function = generic_utils.func_load(

  File "C:\Users\hussa\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 457, in func_load
    code = marshal.loads(raw_code)

**ValueError: bad marshal data (unknown type code)**

Process finished with exit code 1"

0 个答案:

没有答案