JapaneseVowels
这个数据集在训练的时候总是不能正常保存best_mdoel.hdf5
class Classifier_INCEPTION:
def __init__(self, output_directory, input_shape, nb_classes, verbose=False, build=True, batch_size=64,
nb_filters=32, use_residual=True, use_bottleneck=True, depth=6, kernel_size=41, nb_epochs=1500):
self.output_directory = output_directory
self.nb_filters = nb_filters
self.use_residual = use_residual
self.use_bottleneck = use_bottleneck
self.depth = depth
self.kernel_size = kernel_size - 1
self.callbacks = None
self.batch_size = batch_size
self.bottleneck_size = 32
self.nb_epochs = nb_epochs
if build == True:
# input_shape表示(输入时序数据的长度,输入数据的通道数),univariate的channel为1
# nb_classes表示几分类问题
self.model = self.build_model(input_shape, nb_classes)
if (verbose == True):
self.model.summary()
self.verbose = verbose
self.model.save_weights(self.output_directory + 'model_init.hdf5')
def build_model(self, input_shape, nb_classes):
# input_shape表示(输入时序数据的长度,输入数据的通道数),univariate的channel为1
input_layer = keras.layers.Input(input_shape)
# print('raw',input_layer.shape)
x = input_layer
input_res = input_layer
# depth默认设置为6
for d in range(self.depth):
# print(d)
x = self._inception_module(x)
# 当depth=2和depth=5时,分别进行残差传递
if self.use_residual and d % 3 == 2:
x = self._shortcut_layer(input_res, x)
input_res = x
gap_layer = keras.layers.GlobalAveragePooling1D()(x)
# 表达式keras.layers.Dense(nb_classes, activation='softmax')(gap_layer)表示将gap_layer张量传递给Dense()层作为输入,并应用softmax激活函数生成输出张量。
output_layer = keras.layers.Dense(nb_classes, activation='softmax')(gap_layer)
model = keras.models.Model(inputs=input_layer, outputs=output_layer)
model.compile(loss='categorical_crossentropy', optimizer=tf.keras.optimizers.Adam(),
metrics=['accuracy'])
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='loss', factor=0.5, patience=50,
min_lr=0.0001)
print('none??!')
file_path = self.output_directory + 'best_model.hdf5'
model_checkpoint = keras.callbacks.ModelCheckpoint(filepath=file_path, monitor='loss',
save_best_only=True)
print(file_path)
self.callbacks = [reduce_lr, model_checkpoint]
return model
def fit(self, x_train, y_train, x_val, y_val, y_true, plot_test_acc=False):
# if len(tf.compat.v1.keras.backend.tensorflow_backend._get_available_gpus()) == 0:
if len(tf.config.list_physical_devices('GPU'))==0:
print('error no gpu')
exit()
# x_val and y_val are only used to monitor the test loss and NOT for training
if self.batch_size is None:
mini_batch_size = int(min(x_train.shape[0] / 10, 16))
else:
mini_batch_size = self.batch_size
start_time = time.time()
if plot_test_acc:
hist = self.model.fit(x_train, y_train, batch_size=mini_batch_size, epochs=self.nb_epochs,
verbose=self.verbose, validation_data=(x_val, y_val), callbacks=self.callbacks)
else:
hist = self.model.fit(x_train, y_train, batch_size=mini_batch_size, epochs=self.nb_epochs,
verbose=self.verbose, callbacks=self.callbacks)
duration = time.time() - start_time
self.model.save(self.output_directory + 'last_model.hdf5')
y_pred = self.predict(x_val, y_true, x_train, y_train, y_val,
return_df_metrics=False)
# save predictions
np.save(self.output_directory + 'y_pred.npy', y_pred)
# convert the predicted from binary to integer
y_pred = np.argmax(y_pred, axis=1)
df_metrics = save_logs(self.output_directory, hist, y_pred, y_true, duration,
plot_test_acc=plot_test_acc)
keras.backend.clear_session()
return df_metrics
主要是这两句:
model_checkpoint = keras.callbacks.ModelCheckpoint(filepath=file_path, monitor='loss',
save_best_only=True)
self.callbacks = [reduce_lr, model_checkpoint]
hist = self.model.fit(x_train, y_train, batch_size=mini_batch_size, epochs=self.nb_epochs,
verbose=self.verbose, validation_data=(x_val, y_val), callbacks=self.callbacks)
现在问题是只有JapaneseVowels
有问题!其他数据集没有问题。。