python - 当输入的 dtype 为 uint8 时,tf.keras.Model.save 抛出 Not JSON Serialized

标签 python tensorflow keras

TensorFlow:1.14.0

我正在尝试修改 tf.keras.applications 中的 MobileNetV2 实现以接受 uint8 输入而不是 float32。然后,我将转换添加到 float32 并重新缩放到 [-1,1] 作为模型的前几层。这个想法是让转换成为推理图的一部分,而不是必须在 TF 之外进行。

为此,以下是 mobilenet_v2.py 中相关更改的行:

if input_tensor is None:
    img_input = layers.Input(shape=input_shape, dtype="uint8")
...
x = backend.cast(img_input, dtype="float32")
x *= 1 / 127.5
x -= 1.0

first_block_filters = _make_divisible(32 * alpha, 8)
x = layers.ZeroPadding2D(padding=correct_pad(backend, x, 3),
                         name='Conv1_pad')(x)
...

model.fit 运行没有问题,但是当我尝试使用 model.save 保存模型时,它会抛出

model.save(path.join(best_model_dir, "model.h5"))
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py", line 1211, in save
  saving.save_model(self, filepath, overwrite, include_optimizer, save_format)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py", line 113, in save_model
model, filepath, overwrite, include_optimizer)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/hdf5_format.py", line 101, in save_model_to_hdf5
default=serialization.get_json_type).encode('utf8')
File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/serialization.py", line 69, in get_json_type
raise TypeError('Not JSON Serializable:', obj)
TypeError: ('Not JSON Serializable:', b'\n\x04Cast\x12\x04Cast\x1a\x07input_1*\n\n\x04SrcT\x12\x020\x04*\x0e\n\x08Truncate\x12\x02(\x00*\n\n\x04DstT\x12\x020\x01')

我还尝试创建自己的 keras InputLayer,然后进行转换和重新缩放,并用它替换未修改的 MobileNetV2 模型的输入层,但最终遇到了相同的异常。

[编辑] 这是我的替代方法的代码,我认为这确实是更干净的方法。但错误仍然相同。

images = layers.Input(shape=(56, 112, 3), dtype="uint8", name="input")
x = tf.keras.backend.cast(images, dtype="float32")
x *= 1 / 127.5
x -= 1.0

# Prepare the model trunk.
mobilenet = tf.keras.applications.MobileNetV2(input_shape=(56, 112, 3), include_top=False,
                                              weights="imagenet")
mobilenet.layers.pop(0)
x = mobilenet(x)

... add output layers ...

model = tf.keras.Model(inputs=images, outputs=outputs)

[/编辑]

你知道我做错了什么吗?一旦我将输入的数据类型更改为 float32 以外的任何类型,序列化似乎就会失败。

提前致谢!

最佳答案

在输入损失函数或其他相关位置时,尝试将 .numpy() 函数用于 tf.Variable 方法。

这可能适用于前: x = backend.cast(img_input, dtype="float32").numpy()

我在从 TensorFlow: 1.4 代码更改为 TensorFlow: 2.1.0 时遇到了类似的问题,并且 .numpy() 解决了问题。 我用于解决类似问题的示例代码如下:

alpha = K.variable(value=0.98, dtype="float32", name="alpha")
...
loss_weights=[alpha.numpy()] # added .numpy()

有关解决方案的更多详细信息,请参阅此处: https://github.com/tensorflow/tensorflow/issues/28799#issuecomment-739474591

关于python - 当输入的 dtype 为 uint8 时,tf.keras.Model.save 抛出 Not JSON Serialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56804384/

相关文章:

tensorflow - TensorFlow 中的实验性是什么意思?

python - 如何指定 keras lstm 的输出标签

python - 生成缓冲区半径多边形 - 可能的投影问题

python - 从 Pyspark 中的多列获取值

python - 如何在CNN keras模型中将两个不同维度的输入(keras_tensor元素)相乘?

keras - Keras 中 CNN 回归任务中奇怪的均方根误差行为

python - Keras:使用自定义损失函数加载模型时出错

python - 发布数据应该是字节。不能是 str 类型

python - 检查包是否从源代码树中导入

python - 如果第一维等于 1,则使用 tf.cond 压缩第一维