【问题标题】:What is the difference between the file extensions .h5 .hdf5 and .ckpt and which one should I use?文件扩展名 .h5 .hdf5 和 .ckpt 有什么区别,我应该使用哪一个?
【发布时间】:2021-02-08 13:17:21
【问题描述】:

我试图在每个检查点将我的 CNN 保存到一个文件中。但是,我应该使用哪个扩展名作为我的文件目录?我还需要在代码末尾调用model.save(filepath),还是我的模型会被ModelCheckpoint()自动保存?

我将模型保存为 .h5 文件,但我不知道是否应该更改它。

from keras import Sequential
from keras_preprocessing.image import ImageDataGenerator
from keras.layers import *
from keras.callbacks import ModelCheckpoint
import numpy as np
import os

img_size = 500 # number of pixels for width and height

#Random Seed
np.random.seed(12321)


training_path = os.getcwd() + "/cats and dogs images/train"
testing_path = os.getcwd() + "/cats and dogs images/test"

#Defines the Model
model = Sequential([
        Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same", input_shape=(img_size,img_size,3)),
        MaxPool2D(pool_size=(2,2), strides=2),
        Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same"),
        MaxPool2D(pool_size=(2,2), strides=2),
        Flatten(),
        Dense(32, activation="relu"),
        Dense(1, activation="sigmoid")
])


#Scales the pixel values to between 0 to 1
datagen = ImageDataGenerator(rescale=1.0/255.0)

#Prepares Training Data
training_dataset = datagen.flow_from_directory(directory = training_path, target_size=(img_size,img_size), classes = ["cat","dog"], batch_size = 19)

#Prepares Testing Data
testing_dataset = datagen.flow_from_directory(directory = testing_path, target_size=(img_size,img_size), classes = ["cat","dog"], batch_size = 19)


#Compiles the model
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])


#Checkpoint
checkpoint = ModelCheckpoint("trained_model.h5", monitor='loss', verbose=1, save_best_only=True, mode='min', period=1)

#Fitting the model to the dataset (Training the Model)
model.fit(x = training_dataset, steps_per_epoch = 658, validation_data=testing_dataset, validation_steps=658, epochs = 10, callbacks=[checkpoint], verbose = 1)


# evaluate model on training dataset
acc = model.evaluate_generator(training_dataset, steps=len(training_dataset), verbose=0)
print("Accuracy on training dataset:")
print('> %.3f' % (acc * 100.0))


#evaluate model on testing dataset
acc = model.evaluate_generator(testing_dataset, steps=len(testing_dataset), verbose=0)
print("Accuracy on testing dataset:")
print('> %.3f' % (acc * 100.0))

##Saving the Model:
#model.save("trained model.h5")
#print("Saved model to disk")

【问题讨论】:

    标签: keras neural-network conv-neural-network


    【解决方案1】:

    文件扩展名 .h5、.hdf5 和 .ckpt 有什么区别?

    .h5 和 .hdf5

    根据this .h5 和 .hdf5 基本相同,都是以分层数据格式(HDF)保存的数据文件,包含科学数据的多维数组。

    根据this,使用该格式保存模型会导致使用以下内容保存模型:

    1. 权重值。
    2. 模型的架构。
    3. 模型的训练配置(传递给 .compile() 方法的内容)
    4. 优化器及其状态(如果有)(这使您可以从中断的地方重新开始训练)

    .ckpt

    checkpoint 的简称,顾名思义就是模型在训练中达到一定条件(低于一定损失值或高于一定准确率值)后保存模型的状态。

    将模型保存为 .ckpt 有其缺点,因为它只保存变量或图形的权重,因此您需要拥有完整的架构和函数,用于将这些权重和变量加载到架构中并构建和使用模型. (基本上是代码)

    此格式主要用于您想要恢复训练时,并允许您自定义保存的检查点并加载它们。 (允许不断改进模型并根据结果更改参数,从而允许从不同的检查点创建不同的模型)。

    我应该使用哪个扩展?

    取决于您训练模型的目标,如果您在训练过程中并进行了很多实验,我建议您将模型保存为 .ckpt 格式。

    如果您已经完成了模型的试验和最终确定,我建议您将其保存为 .h5 格式,以便您可以加载并使用它,而无需使用代码来创建模型架构。

    我是否还需要在代码末尾调用 model.save(filepath) 或者我的模型是否会被 ModelCheckpoint() 自动保存?

    您可以同时调用两者,但我建议将 ModelCheckpoint() 中的扩展名设置为 .ckpt,以便您可以在训练过程中保存尽可能高的模型状态,并在完成训练后调用 model.save(filepath)但作为 .h5 格式,因此在训练后模型应该保存并在任何地方使用,而不需要原始架构代码。

    这样您就可以选择增强训练和加载 .ckpt 模型,或者如果您对最终结果感到满意,可以使用 .h5 模型作为模型的最终版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2012-01-16
      • 2021-04-15
      • 1970-01-01
      • 2011-10-10
      • 2020-05-10
      相关资源
      最近更新 更多