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

时间:2019-10-01 23:43:02

标签: python tensorflow keras deep-learning

我制作了一个自定义生成器,在训练过程中,需要根据真实标签对模型进行预测,然后对模型进行一些计算。因此,我先保存模型,然后在当前状态下调用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.predict(model)中调用custom_generator()

,因此出现以下错误

错误:

  

ValueError:张量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 个答案:

答案 0 :(得分:1)

您最好的选择是通过train_on_batchfit编写一个自定义火车循环;前者的唯一劣势是使用use_multiprocessing=True或使用回调-情况并非如此。以下是train_on_batch的实现-如果您改用fit(用于多处理,回调等),请确保只喂一个批次一次并提供 no 验证数据(改为使用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)

[此处是定制火车循环]


奖金代码:要获取 any 层的输出,请在下面使用:

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