简体   繁体   中英

TypeError: can't pickle _thread.lock objects while dumping nn4_small2_pretrained model from Keras-Openface Project

I was trying to implement Facial Recognition using the pretrained models from Keras-Openface Project and amazingly explained and implimented by Martin Krasser here

The OpenFace project provides pre-trained models that were trained with the public face recognition datasets FaceScrub and CASIA-WebFace. The Keras-OpenFace project converted the weights of the pre-trained nn4.small2.v1 model to CSV files which were then converted here to a binary format that can be loaded by Keras with load_weights

The Code is simply :

nn4_small2_pretrained = create_model()
nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5')

Before going to the error to give more insight on what's happening in create_model(), you can go through the code here

Now I need nn4_small2_pretrained in my predict.py file (It is initially a part of train.py to train my custom images), but if I do

from train import nn4_small2_pretrained

Or write the code

nn4_small2_pretrained = create_model()
nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5')

all over again, then the prediction file takes a lot of time to compile as it goes through the whole process again. So to resolve this, I tried to dump the model in a pickle file like so

# Save the nn4 pretrained model to pickle file
f = open('pretrained_model.pickle', 'wb')
pickle.dump(nn4_small2_pretrained, f)

When I run the code it gives me this error

    File "train.py", line 24, in <module>
        pickle.dump(nn4_small2_pretrained, f)
TypeError: can't pickle _thread.lock objects

I have started with Deel Learning Models and Pickle recently and I cannot figure out what's wrong. Any help would be much appreciated.

Thanks.

I see that the create_model() creates an instance of Keras Model. If it is a Keras Model then you can save the model using model.save(filepath) . Refer this link for other options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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