提问者:小点点

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


我做了一个自定义生成器,在训练期间,我需要我的模型的预测,在它被训练到真正的标签之前,对它做一些计算。因此,我先保存模型,然后在当前状态上调用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

由于调用model,我得到了以下错误。在自定义\u生成器()中预测(模型)

错误:

ValueError:Tensor Tensor(“dense_2/Softmax:0”,shape=(?,200),dtype=float32)不是此图的元素。

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

这是我的模型:

#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)

共1个答案

匿名用户

您的最佳选择可能是通过批处理train\u或fit编写自定义列车循环;只有当使用_multiprocessing=True或使用回调时,前者才处于不利地位——事实并非如此。下面是一个使用批上训练的实现-如果改用fit(用于多处理、回调等),请确保一次只反馈一个批,并且不提供任何验证数据(改用模型。评估)-否则控制流中断。(此外,自定义回调是一种有效的、但涉及的替代方法)

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