我对python和机器学习比较陌生,我正在尝试对胸部x射线扫描进行分类,我创建了一个模型来做这件事。然而,当我试图保存模型时,我得到了错误:
TypeError: Unable to serialize [2.0896919 2.1128857 2.1081853] to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
完整的堆栈跟踪是:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~AppDataLocalTempipykernel_220923924096485.py in <module>
1 working_dir=os.getcwd()
2 subject='chest scans'
----> 3 save_model(subject, classes, img_size, f1score, working_dir)
~AppDataLocalTempipykernel_22092541087647.py in save_model(subject, classes, img_size, f1score, working_dir)
3 save_id=f'{name}-{f1score:5.2f}.h5'
4 model_save_loc=os.path.join(working_dir, save_id)
----> 5 model.save(model_save_loc)
6 msg= f'model was saved as {model_save_loc}'
7 print_in_color(msg, (0,255,255), (100,100,100)) # cyan foreground
~anaconda3libsite-packageskerasutilstraceback_utils.py in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
~anaconda3libjson__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
232 if cls is None:
233 cls = JSONEncoder
--> 234 return cls(
235 skipkeys=skipkeys, ensure_ascii=ensure_ascii,
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
~anaconda3libjsonencoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
~anaconda3libjsonencoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
TypeError: Unable to serialize [2.0896919 2.1128857 2.1081853] to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
我已经创建了这个函数来保存模型:
def save_model(subject, classes, img_size, f1score, working_dir):
name=subject + '-' + str(len(classes)) + '-(' + str(img_size[0]) + ' X ' + str(img_size[1]) + ')'
save_id=f'{name}-{f1score:5.2f}.h5'
model_save_loc=os.path.join(working_dir, save_id)
model.save(model_save_loc)
msg= f'model was saved as {model_save_loc}'
print_in_color(msg, (0,255,255), (100,100,100)) # cyan foreground
我已经为tensorflow安装了以下包。
tensorboard 2.8.0
tensorboard-data-server 0.6.1
tensorboard-plugin-wit 1.8.1
tensorflow 2.8.1
tensorflow-estimator 2.8.0
tensorflow-io-gcs-filesystem 0.28.0
termcolor 1.1.0
keras 2.8.0
keras-nightly 2.5.0.dev2021032900
Keras-Preprocessing 1.1.2
任何帮助将是伟大的!我真的无法将tensorflowEagerTensor
转换为我的ML模型的Json文件。谢谢!
我试图保存我创建的ml模型,我失败了,它给了我一个错误,当它转换为json文件
所以我试图将tensorflow从V2.10降级到V2.9.1,这个函数工作得很好。换句话说,这是2.10.0中的一个bug。希望它能帮助,请修复这个错误V2.10
(使用TensorFlow 2.10.1)
尝试检查您是否将TF张量作为任何期望NumPy数组的Keras对象的参数。
我不知道这是否是一个通用的TF/Keras问题,但我在为嵌入层实例化初始化器时遇到了这个问题。如果是initializer = tf.keras.initializers.Constant(c)
,则c
必须是NumPy数组。如果c
是TF张量,模型训练良好,但在序列化过程中会产生OP的错误。