一直报错
Traceback (most recent call last):
File "/home/shuangyanli/seizure-prediction-CNN/mainconfusion.py", line 167, in <module>
main(dataset=args.dataset, build_type=args.mode)
File "/home/shuangyanli/seizure-prediction-CNN/mainconfusion.py", line 102, in main
model.fit(X_train, y_train, X_val, y_val)
File "/home/shuangyanli/seizure-prediction-CNN/models/cnn_mvit.py", line 150, in fit
self.model.fit(X_train, Y_train, batch_size=self.batch_size,
File "/data7/shuangyanLi/AnaConda3/envs/tf39/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/home/shuangyanli/seizure-prediction-CNN/models/customCallbacks.py", line 97, in on_epoch_end
self.model.save_weights(filepath, overwrite=True) # 7.26
File "/data7/shuangyanLi/AnaConda3/envs/tf39/lib/python3.9/site-packages/h5py/_hl/group.py", line 183, in create_dataset
dsid = dataset.make_new_dset(group, shape, dtype, data, name, **kwds)
File "/data7/shuangyanLi/AnaConda3/envs/tf39/lib/python3.9/site-packages/h5py/_hl/dataset.py", line 165, in make_new_dset
dset_id = h5d.create(parent.id, name, tid, sid, dcpl=dcpl, dapl=dapl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5d.pyx", line 135, in h5py.h5d.create
ValueError: Unable to create dataset (name already exists)
将保存下来的h5文件读取一下
code:
>>> import h5py
>>> import tensorflow as tf
>>> path = 'weights_3_cv.h5'
>>> from tensorflow.keras.models import Sequential, Model
>>> mdoel.load_weights(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mdoel' is not defined
>>> mdoel=load_weights(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'load_weights' is not defined
>>> from keras.models import load_model
>>> model = load_model(path)
error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/data7/shuangyanLi/AnaConda3/envs/tf39/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/data7/shuangyanLi/AnaConda3/envs/tf39/lib/python3.9/site-packages/keras/utils/generic_utils.py", line 562, in class_and_config_for_serialized_keras_object
raise ValueError(
ValueError: Unknown layer: MViT. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
参考链接:
https://stackoverflow.com/questions/50837728/valueerror-unknown-layer-capsulelayer
添加图层的初始化参数
添加了但是没有用
换一种方式:
https://stackoverflow.com/questions/72776335/valueerror-unable-to-create-dataset-name-already-exists-when-using-modelcheck
将 ‘.h5’ 保存为 ‘.tf’ 但是还是报错,这样 .tf是一个文件
最后怎么解决的?
答:
没有保存h5文件
而是:
checkpoint_save_path = "weights_%s_%s/" %(self.target, self.mode)
checkpoint_callback = keras.callbacks.ModelCheckpoint(
checkpoint_save_path,
monitor="val_accuracy",
save_best_only=True,
save_weights_only=True,
)