1. 程式人生 > >深度學習BiGan學習筆記,keras版

深度學習BiGan學習筆記,keras版

對抗生成網路Gan變體集合 keras版本
bigan(Bidirectional GAN 雙向Gan)
http://link.zhihu.com/?target=https://arxiv.org/abs/1605.09782
具有生成器G,編碼器encoder,判別器D
作用:
1.可以使用noise經過生成器G生成圖片(這一點和GAN類似)
2.隨便給一張圖片,經過Encoder,再經過生氣G,可以生成相似的圖片(新特點)

程式碼位置:
https://github.com/eriklindernoren/Keras-GAN/tree/master/bigan

生成器:

def build_generator
(self): model = Sequential() model.add(Dense(512, input_dim=self.latent_dim)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(np.prod(self.img_shape), activation=
'tanh')) model.add(Reshape(self.img_shape)) model.summary() z = Input(shape=(self.latent_dim,)) gen_img = model(z) return Model(z, gen_img)

編碼器:

def build_encoder(self):
	model = Sequential()
	model.add(Flatten(input_shape=self.img_shape))
	model.add(Dense(512))
	model.add(LeakyReLU(alpha=
0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(self.latent_dim)) model.summary() img = Input(shape=self.img_shape) z = model(img) return Model(img, z)

判別器:

def build_discriminator(self):
	z = Input(shape=(self.latent_dim, ))
	img = Input(shape=self.img_shape)
	d_in = concatenate([z, Flatten()(img)])
	
	model = Dense(1024)(d_in)
	model = LeakyReLU(alpha=0.2)(model)
	model = Dropout(0.5)(model)
	model = Dense(1024)(model)
	model = LeakyReLU(alpha=0.2)(model)
	model = Dropout(0.5)(model)
	model = Dense(1024)(model)
	model = LeakyReLU(alpha=0.2)(model)
	model = Dropout(0.5)(model)
	validity = Dense(1, activation="sigmoid")(model)
	
	return Model([z, img], validity)

訓練圖:
在這裡插入圖片描述