对抗性判别域适应(ADDA)

时间:2018-05-28 17:39:11

标签: python tensorflow keras keras-layer generative-adversarial-network

我正在尝试在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后端。

非常感谢!

2 个答案:

答案 0 :(得分:1)

问题在于训练部分 - 我加载到目标模型预训练源模型(load_model)并且因为它改变了对生成器模型的引用而产生了问题。我应该使用load_weights

而不是load_model

因此,加载预先训练的模型是有效的,但不会引用参考文献:

    source_model = load_model(modelName)
    target_model.set_weights(source_model.get_weights())

答案 1 :(得分:0)

@JecaJeca您是否愿意共享其余代码?