JSONDecodeError:加载 tf.Keras 模型时的期望值
Posted
技术标签:
【中文标题】JSONDecodeError:加载 tf.Keras 模型时的期望值【英文标题】:JSONDecodeError: Expecting value when loading tf.Keras model 【发布时间】:2021-11-24 16:27:09 【问题描述】:当我尝试加载经过训练的 tf.keras
模型时,我收到错误消息:JSONDecodeError: Expecting value
。
该问题不可重现,即当我尝试加载已在集群上训练并下载到计算机的模型时出现错误。当我尝试只编译、保存和加载相同的模型时,没有出现错误。
任何想法可能是什么问题?
以下代码有效(与加载经过训练的模型相反):
import tensorflow as tf
from tensorflow import keras as ks
import numpy as np
import os
def UNet(n_classes, input_shape = (256, 256, 3), dropout = 0.05,
ops = "activation" : "relu",
"padding" : "same",
"kernel_initializer" : "he_normal"
):
# input layer
inputz = ks.layers.Input(shape = input_shape)
# encoder part
## 1st convolution
c1 = ks.layers.Conv2D(64, (3, 3), **ops)(inputz)
c1 = ks.layers.Conv2D(64, (3, 3), **ops)(c1)
## 1st max pooling
p1 = ks.layers.MaxPooling2D(pool_size = (2, 2))(c1)
## 2nd convolution
c2 = ks.layers.Conv2D(128, (3, 3), **ops)(p1)
c2 = ks.layers.Conv2D(128, (3, 3), **ops)(c2)
## 2nd max pooling
p2 = ks.layers.MaxPooling2D(pool_size = (2, 2))(c2)
## 3rd convolution
c3 = ks.layers.Conv2D(256, (3, 3), **ops)(p2)
c3 = ks.layers.Conv2D(256, (3, 3), **ops)(c3)
## 3rd max pooling
p3 = ks.layers.MaxPooling2D(pool_size = (2, 2))(c3)
## 4th convolution
c4 = ks.layers.Conv2D(512, (3, 3), **ops)(p3)
c4 = ks.layers.Conv2D(512, (3, 3), **ops)(c4)
## Drop
d4 = ks.layers.Dropout(dropout)(c4)
## 4th max pooling
p4 = ks.layers.MaxPooling2D(pool_size = (2, 2))(d4)
## 5th convolution
c5 = ks.layers.Conv2D(1024, (3, 3), **ops)(p4)
c5 = ks.layers.Conv2D(1024, (3, 3), **ops)(c5)
## Drop
d5 = ks.layers.Dropout(dropout)(c5)
# decoder part
## 1st up convolution
us6 = ks.layers.UpSampling2D(size = (2, 2))(d5)
up6 = ks.layers.Conv2D(512, (2, 2), **ops)(us6)
## merge
ct6 = ks.layers.concatenate([d4, up6], axis = 3)
uc6 = ks.layers.Conv2D(512, (3, 3), **ops)(ct6)
uc6 = ks.layers.Conv2D(512, (3, 3), **ops)(uc6)
## 2nd up convolution
us7 = ks.layers.UpSampling2D(size = (2, 2))(uc6)
up7 = ks.layers.Conv2D(256, (2, 2), **ops)(us7)
## merge
ct7 = ks.layers.concatenate([c3, up7], axis = 3)
uc7 = ks.layers.Conv2D(256, (3, 3), **ops)(ct7)
uc7 = ks.layers.Conv2D(256, (2, 2), **ops)(uc7)
## 3rd up convolution
us8 = ks.layers.UpSampling2D(size = (2, 2))(uc7)
up8 = ks.layers.Conv2D(128, (2, 2), **ops)(us8)
## merge
ct8 = ks.layers.concatenate([c2, up8], axis = 3)
uc8 = ks.layers.Conv2D(128, (3, 3), **ops)(ct8)
uc8 = ks.layers.Conv2D(128, (3, 3), **ops)(uc8)
## 4th up convolution
us9 = ks.layers.UpSampling2D(size = (2, 2))(uc8)
up9 = ks.layers.Conv2D(64, (2, 2), **ops)(us9)
## merge
ct9 = ks.layers.concatenate([c1, up9], axis = 3)
uc9 = ks.layers.Conv2D(64, (3, 3), **ops)(ct9)
uc9 = ks.layers.Conv2D(64, (3, 3), **ops)(uc9)
uc9 = ks.layers.Conv2D(2, (3, 3), **ops)(uc9)
# output layer
if n_classes > 2:
activ = "softmax"
else:
activ = "sigmoid"
outputz = ks.layers.Conv2D(n_classes, 1, activation = activ)(uc9)
model = ks.Model(inputs = [inputz], outputs = [outputz])
print(model.summary())
print(f'Total number of layers: len(model.layers)')
return model
# get model
model = UNet(n_classes = 5)
class UpdatedMeanIoU(tf.keras.metrics.MeanIoU):
def __init__(self,
y_true = None,
y_pred = None,
num_classes = None,
name = None,
dtype = None):
super(UpdatedMeanIoU, self).__init__(num_classes = num_classes,
name = name, dtype = dtype)
def update_state(self, y_true, y_pred, sample_weight = None):
y_pred = tf.math.argmax(y_pred, axis = -1)
return super().update_state(y_true, y_pred, sample_weight)
mIoU = UpdatedMeanIoU(num_classes = 5)
lr_sched = ks.optimizers.schedules.ExponentialDecay(
initial_learning_rate = 1e-3,
decay_steps = np.floor(50),
decay_rate = 0.995)
optimizer = ks.optimizers.RMSprop(learning_rate = lr_sched, clipnorm = 1)
lozz = ks.losses.SparseCategoricalCrossentropy()
model.compile(optimizer = optimizer, loss = lozz,
metrics = [mIoU])
model.save("G:\\mot", save_format = "tf")
os.chdir("G:\\mot")
trained_model = ks.models.load_model("G:\\mot", custom_objects = "UpdatedMeanIoU": mIoU)
不起作用的部分代码仅包含model.compile
和model.save
之间的model.fit
。
损坏的模型可以在here找到。
这是完整的错误信息:
trained_model = ks.models.load_model(moddir,\
custom_objects = "UpdatedMeanIoU": mIoU)
Traceback (most recent call last):
File "<ipython-input-140-4d44f44a3739>", line 1, in <module>
trained_model = ks.models.load_model(moddir,\
File "c:\users\manuel\python\lib\site-packages\tensorflow\python\keras\saving\save.py", line 206, in load_model
return saved_model_load.load(filepath, compile, options)
File "c:\users\manuel\python\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 155, in load
keras_loader.finalize_objects()
File "c:\users\manuel\python\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 626, in finalize_objects
self._reconstruct_all_models()
File "c:\users\manuel\python\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 645, in _reconstruct_all_models
self._reconstruct_model(model_id, model, layers)
File "c:\users\manuel\python\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 661, in _reconstruct_model
config = json_utils.decode(
File "c:\users\manuel\python\lib\site-packages\tensorflow\python\keras\saving\saved_model\json_utils.py", line 62, in decode
return json.loads(json_string, object_hook=_decode_helper)
File "c:\users\manuel\python\lib\json\__init__.py", line 359, in loads
return cls(**kw).decode(s)
File "c:\users\manuel\python\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\users\manuel\python\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
编辑:
我可以将模型加载到我训练它的服务器上(在单独的 Python 会话中)。但是,下载文件后,我无法在计算机上执行相同操作。该模型是否存在一些隐藏文件?或者不同的操作系统或不同的模块版本会不会有问题?
【问题讨论】:
这能回答你的问题吗? JSONDecodeError: Expecting value: line 1 column 1 (char 0) 【参考方案1】:您是否尝试过最新的 TF/keras 版本(比如 2.7)?我没有注意到最近版本的这个错误。另外,请分享一个玩具数据来运行 model.fit(没有这种保存和加载按预期工作)。 Here 是我参考的要点。谢谢
【讨论】:
以上是关于JSONDecodeError:加载 tf.Keras 模型时的期望值的主要内容,如果未能解决你的问题,请参考以下文章
js动态加载网页,json.loads报错 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
在python中使用for循环时出现JSONDecodeError [重复]
PythonAnywhere 上的 JSONDecodeError
yfinance 不工作 - 接收 json.decoder.JSONDecodeError