加载Joblib文件使用自定义指标

发布于 2025-01-26 10:40:13 字数 1344 浏览 4 评论 0 原文

我正在培训多种型号(在Google Colab上),并使用Joblib保存它们。然后,我正在下载这些文件并使用Joblib加载它们,但是我会遇到以下错误;

无法还原类型_tf_keras_metric的自定义对象,

我正在使用量表= ['fecuctiacy',特异性,tf.keras.metrics.precision()]编译模型,其中特定性是从 https://www.sabinasz.net/unbalanced-classes-classes-classes-machine-machine-learning/

mobilenet_model = MobileNetV3Small(include_top=True, weights=None, classes=2, input_shape=(100, 100, 3))
mobilenet_model.compile(loss='binary_crossentropy', metrics=['accuracy', specificity, tf.keras.metrics.Precision()])
trained_model  = mobilenet_model.fit(X_train, y_train)

joblib.dump(trained_model, 'mobilenet_model.joblib')

我尝试使用keras.models.load_model加载它们,但是我得到了一个错误打开文件(未找到文件签名),该文件根据表示文件已损坏(我想Joblib保存模型与Keras的Save_model函数不兼容) 。

我尝试使用keras.models.save_model保存文件并传递custic_objects = {'特殊性':特殊性,'precision':tf.keras.metrics.precision()},但该模型似乎没有预期的权重,因为当我评估它时,它的精度仅为0.5,当我训练时获得0.9时。

那我该怎么办?

I am training multiple models (on Google Colab) and saving them using joblib. I am then downloading these files and loading them using joblib, but I am getting the following error;

Unable to restore custom object of type _tf_keras_metric

I am compiling the models using metrics=['accuracy', specificity, tf.keras.metrics.Precision()], where specificity is a custom metric function from https://www.sabinasz.net/unbalanced-classes-machine-learning/.

mobilenet_model = MobileNetV3Small(include_top=True, weights=None, classes=2, input_shape=(100, 100, 3))
mobilenet_model.compile(loss='binary_crossentropy', metrics=['accuracy', specificity, tf.keras.metrics.Precision()])
trained_model  = mobilenet_model.fit(X_train, y_train)

joblib.dump(trained_model, 'mobilenet_model.joblib')

I tried to load them using keras.models.load_model but I get a Error opening file (File signature not found) which according to Error opening file in H5PY (File signature not found) means the file is corrupt (I imagine that the way joblib saves models is not compatible with keras's save_model function).

I tried using keras.models.save_model to save a file and passing in custom_objects={'specificity': specificity, 'precision': tf.keras.metrics.Precision()}, but the model seems to be saved without the pretrained weights because when I evaluate it, it only has 0.5 accuracy, when it was getting 0.9 when I trained it.

So what can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文