尝试将我的CNN模型保存在Pickle文件中,但出现TypeError:无法对_thread._local对象进行pickle

时间:2019-12-26 07:23:32

标签: python service deployment pickle faster-rcnn

尝试过的2 methods都出现相同的错误,请帮助我将其保存为pickle文件,以便以后使用该模型。

我正在寻找使用该模型创建service.exe的方法,请提出一种最佳方法,因为我对数据科学比较陌生

result = classifier.predict(kyc_test_image)
kyc_training_set.class_indices
if result[0][0] == 1:
    prediction = 'pan'
else:
    prediction = 'driv'

kyc_model = classifier

import pickle
modelname = 'cnn_kyc'
outfile = open(modelname,'wb')
pickle.dump(kyc_model,outfile)
outfile.close()

TypeError                                 Traceback (most recent call last)
<ipython-input-35-865fb4ce2e0b> in <module>
----> 1 pickle.dump(kyc_model,outfile)
      2 outfile.close()

TypeError: can't pickle _thread._local objects

第二种方法

from sklearn.externals import joblib

joblib.dump(kyc_model, 'cnn_kyc.pkl')

cnn_from_joblib = joblib.load('cnn_kyc.pkl') 

cnn_from_joblib.predict(kyc_test_image)

1 个答案:

答案 0 :(得分:0)

导入joblib#以保存模型

要保存

path_name = "../../../"    # some path


model_name = 'model_name.sav' # I think saving it to sav format maybe will help y


joblib.dump(model, path_name + model_name)

# For Loading
joblib.load(path_name + model_name)