gpt4 book ai didi

python - Keras:如何在训练期间在自定义生成器中获得模型预测(或最后一层输出)?

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:38 25 4
gpt4 key购买 nike

我制作了一个自定义生成器,在训练期间我需要模型的预测,在针对真实标签进行训练之前对其进行一些计算。因此,我先保存模型,然后根据当前状态调用 model.predict()

from keras.models import load_model
def custom_generator(model):
while True:
state, target_labels = next(train_it)

model.save('my_model.h5')
#pause training and do some calculations on the output of the model trained so far
print(state)
print(target_labels)
model.predict(state)
#resume training
#model = load_model('my_model.h5')

yield state, target_labels

model3.fit_generator(custom_generator(model3), steps_per_epoch=1, epochs = 10)
loss = model3.evaluate_generator(test_it, steps=1)
loss

由于在 custom_generator() 中调用 model.predict(model),我收到以下错误

错误:

ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 200), dtype=float32) is not an element of this graph.

请帮助我如何在训练期间在自定义生成器中获得模型预测(或最后一层输出)。

这是我的模型:

#libraries
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from matplotlib import pyplot
from keras.applications.vgg16 import VGG16

model = VGG16(include_top=False, weights='imagenet')
print(model.summary())

#add layers
z = Conv2D(1, (3, 3), activation='relu')(model.output)
z = Conv2D(1,(1,1), activation='relu')(z)
z = GlobalAveragePooling2D()(z)
predictions3 = Dense(200, activation='softmax')(z)
model3 = Model(inputs=model.input, outputs=predictions3)
for layer in model3.layers[:20]:
layer.trainable = False
for layer in model3.layers[20:]:
layer.trainable = True
model3.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

用于加载训练和测试数据的图像数据生成器

from keras.preprocessing.image import ImageDataGenerator
# create a data generator
datagen = ImageDataGenerator()
# load and iterate training dataset
train_it = datagen.flow_from_directory('DATA/C_Train/', class_mode='categorical', batch_size=1)
test_it = datagen.flow_from_directory('DATA/C_Test/', class_mode='categorical', batch_size=1)

最佳答案

您最好的选择可能是通过 train_on_batchfit 编写自定义训练循环;前者仅在 use_multiprocessing=True 或使用回调时处于劣势 - 但事实并非如此。以下是使用 train_on_batch 的实现 - 如果您使用 fit 代替(用于多处理、回调等),请确保您只提供 一次一个 批处理,并提供 验证数据(使用model.evaluate 代替)——否则控制流中断。 (此外,自定义 Callback 是一种有效但复杂的替代方案)


自定义列车循环

iters_per_epoch = len(train_it) // batch_size
num_epochs = 5
outs_store_freq = 20 # in iters
print_loss_freq = 20 # in iters

iter_num = 0
epoch_num = 0
model_outputs = []
loss_history = []

while epoch_num < num_epochs:
while iter_num < iters_per_epoch:
x_train, y_train = next(train_it)
loss_history += [model3.train_on_batch(x_train, y_train)]

x_test, y_test = next(test_it)
if iter_num % outs_store_freq == 0:
model_outputs += [model3.predict(x_test)]
if iter_num % print_loss_freq == 0:
print("Iter {} loss: {}".format(iter_num, loss_history[-1]))

iter_num += 1
print("EPOCH {} FINISHED".format(epoch_num + 1))
epoch_num += 1
iter_num = 0 # reset counter


完整代码

from keras.models import Sequential
from keras.layers import Dense, Conv2D, GlobalAveragePooling2D
from keras.models import Model
from keras.optimizers import SGD
from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import ImageDataGenerator

model = VGG16(include_top=False, weights='imagenet')
print(model.summary())

#add layers
z = Conv2D(1, (3, 3), activation='relu')(model.output)
z = Conv2D(1,(1,1), activation='relu')(z)
z = GlobalAveragePooling2D()(z)
predictions3 = Dense(2, activation='softmax')(z)
model3 = Model(inputs=model.input, outputs=predictions3)

for layer in model3.layers[:20]:
layer.trainable = False
for layer in model3.layers[20:]:
layer.trainable = True

model3.compile(optimizer=SGD(lr=0.0001, momentum=0.9),
loss='categorical_crossentropy')
batch_size = 1
datagen = ImageDataGenerator()
train_it = datagen.flow_from_directory('DATA/C_Train/',
class_mode='categorical',
batch_size=batch_size)
test_it = datagen.flow_from_directory('DATA/C_Test/',
class_mode='categorical',
batch_size=batch_size)

[在此处自定义火车循环]


奖励代码:要获取任何层的输出,请使用以下代码:

def get_layer_outputs(model, layer_name, input_data, learning_phase=1):
outputs = [layer.output for layer in model.layers if layer_name in layer.name]
layers_fn = K.function([model.input, K.learning_phase()], outputs)
return [layers_fn([input_data,learning_phase])][0]

outs = get_layer_outputs(model, 'dense_1', x_test, 0) # 0 == inference mode

关于python - Keras:如何在训练期间在自定义生成器中获得模型预测(或最后一层输出)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58193588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com