h5py出错
记录下调试程序时候修改的bug
在网上找的程序(这位大佬:https://space.bilibili.com/18161609/video?tid=0&page=2&keyword=&order=pubdate.)。说明下用的TF是2.4.0
我是直接用的大佬的Resnet网络,由于原网络中没有出准确率曲线图,于是我将他VGG程序后面的Keras high level api加过来替换了原本的那部分训练过程。
下面是我调换的程序 `。
# 实例化模型
feature = resnet50(im_width=64, im_height=64, num_classes=9, include_top=False)
# pre_weights_path = './pretrain_weights.ckpt'
# assert len(glob.glob(pre_weights_path + "*")), "cannot find {}".format(pre_weights_path)
# feature.load_weights(pre_weights_path)
# feature.trainable = False
model = tf.keras.Sequential([feature,
tf.keras.layers.GlobalAvgPool2D(),
tf.keras.layers.Dropout(rate=0.3),
tf.keras.layers.Dense(512, activation="relu"),
tf.keras.layers.Dropout(rate=0.3),
tf.keras.layers.Dense(9),
tf.keras.layers.Softmax()])
model.summary()
# using keras high level api for training
model.compile(
# optimizer=tf.keras.optimizers.SGD(lr=0.001, momentum=0.0, decay=0.0002, nesterov=False),
optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
metrics=["accuracy"])
callbacks = [tf.keras.callbacks.ModelCheckpoint(filepath='./save_weights/myResnet_{epoch:02d}.h5',
save_best_only=True,
save_weights_only=True,
monitor='val_loss'),
]
# tensorflow2.1 recommend to using fit
history = model.fit(x=train_dataset,
steps_per_epoch=train_num // batch_size,
epochs=epochs,
validation_data=val_dataset,
validation_steps=val_num // batch_size,
callbacks=callbacks)
def plot_graphs(history, string):
plt.plot(history.history[string])
plt.plot(history.history['val_' + string])
plt.xlabel('epochs')
plt.ylabel(string)
plt.legend([string, 'val_' + string])
plt.show()
plot_graphs(history, 'accuracy')
plot_graphs(history, 'loss')
在运行过程中,运行完一个epoch就报错:
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/h5f.pyx”, line 78, in h5py.h5f.open
OSError: Unable to open file (file signature not found)
在网上查找好多都是说h5py需要更新,我更新后依然存在这个问题,之后我索性不用h5py。将其换成.ckpt格式完美运行。
修改
将
filepath='./save_weights/myResnet_{epoch:02d}.h5'
替换成
filepath='./save_weights/myResnet_{epoch:02d}.ckpt'
这种应该就是模型权重保存的格式不同。