2

我正在尝试在 Keras 中实现 ADDA。这是我的代码:

class ADDA_Images(object):

def __init__(self,modelInput):
    self.img_rows = 28
    self.img_cols = 28
    self.channels = 3
    self.img_shape = (self.img_rows, self.img_cols, self.channels)

    optimizer = opt.Adam(0.001)

    self.source_generator = self.build_generator(modelInput)
    self.target_generator = self.build_generator(modelInput)

    outputFeatureExtraction = layers.Input(shape = self.target_generator.output_shape[1:])
    self.source_classificator = self.build_classifier(outputFeatureExtraction)


    self.discriminator_model = self.build_discriminator(outputFeatureExtraction)
    self.discriminator_model.compile(optimizer, loss='binary_crossentropy', metrics=['acc'])
    self.discriminator_model.name='disk'

    input = layers.Input(shape=self.img_shape)
    fe_rep = self.source_generator(input)
    cl = self.source_classificator(fe_rep)
    self.source_model = Model(input,cl)
    self.source_model.compile(optimizer, loss='categorical_crossentropy', metrics=['acc'])

    input = layers.Input(shape=self.img_shape)
    fe_rep = self.target_generator(input)
    cl = self.source_classificator(fe_rep)
    self.target_model = Model(input, cl)
    self.target_model.compile(optimizer, loss='categorical_crossentropy', metrics=['acc'])

    self.combined_model = Sequential()
    self.combined_model.add(self.target_generator)
    self.combined_model.add(self.discriminator_model)
    self.combined_model.get_layer('disk').trainable = False
    self.combined_model.compile(optimizer, loss='binary_crossentropy', metrics=['acc'])

    print('Source model')
    self.source_model.summary()

    print('Target model')
    self.target_model.summary()

    print('Discriminator')
    self.discriminator_model.summary()

    print('Combined model')
    self.combined_model.summary()

def build_generator(self,modelInput):

    gen = layers.Conv2D(filters=20, kernel_size=5, padding='valid')(modelInput)
    gen = layers.MaxPooling2D(pool_size=2, strides=2)(gen)
    gen = layers.Conv2D(filters=50, kernel_size=5, padding='valid')(gen)
    gen = layers.MaxPooling2D(pool_size=2, strides=2)(gen)
    gen = layers.Flatten()(gen)

    model = Model(modelInput,gen)
    print('Generator summary')
    model.summary()
    return model

def build_classifier(self,modelInput):

    cl = layers.Dense(3072, activation='relu')(modelInput)
    cl = layers.Dense(2048, activation='relu')(cl)
    cl = layers.Dense(10, activation='softmax')(cl)

    model = Model(modelInput,cl)
    print('Classificatior summary')
    model.summary()
    return model

def build_discriminator(self,modelInput):

    disc = layers.Dense(500, activation='relu')(modelInput)
    disc = layers.Dense(500, activation='relu')(disc)
    disc = layers.Dense(2, activation='softmax')(disc)

    model = Model(modelInput,disc)
    print('Discriminator summary')
    model.summary()
    return model

但是,似乎 target_generator 没有连接到目标模型。我从预训练的源模型中加载目标模型,然后以 ADDA 方式训练鉴别器和组合模型。但是,目标模型没有改变。它始终具有与源模型相同的预测(accs 和 loss)。

以下是模型摘要:

Source model
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 28, 28, 3)         0         
_________________________________________________________________
model_1 (Model)              (None, 800)               26570     
_________________________________________________________________
model_3 (Model)              (None, 10)                8774666   
=================================================================
Total params: 8,801,236
Trainable params: 8,801,236
Non-trainable params: 0
_________________________________________________________________
Target model
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         (None, 28, 28, 3)         0         
_________________________________________________________________
model_2 (Model)              (None, 800)               26570     
_________________________________________________________________
model_3 (Model)              (None, 10)                8774666   
=================================================================
Total params: 8,801,236
Trainable params: 8,801,236
Non-trainable params: 0
_________________________________________________________________
Discriminator
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 800)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 500)               400500    
_________________________________________________________________
dense_5 (Dense)              (None, 500)               250500    
_________________________________________________________________
dense_6 (Dense)              (None, 2)                 1002      
=================================================================
Total params: 1,304,004
Trainable params: 652,002
Non-trainable params: 652,002
_________________________________________________________________
Combined model
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
model_2 (Model)              (None, 800)               26570     
_________________________________________________________________
disk (Model)                 (None, 2)                 652002    
=================================================================
Total params: 678,572
Trainable params: 26,570
Non-trainable params: 652,002

我验证了 target_model 第二层的输出(按规范应该是 target_generator),它与 target_generator 的输出不同(在相同的输入上)。因此,这两个模型似乎没有像摘要中报告的那样连接。

有人可以帮我找出问题所在吗?

我正在使用 Keras 2,Tensorflow 后端。

4

1 回答 1

2

问题出在训练部分——我将预训练的源模型(load_model)加载到目标模型中,这产生了问题,因为它改变了对生成器模型的引用。我应该使用 load_weights 而不是 load_model

因此,加载有效且不会对引用产生问题的预训练模型是:

    source_model = load_model(modelName)
    target_model.set_weights(source_model.get_weights())
于 2018-05-29T16:16:23.420 回答