基于生成对抗性神经网络的潜变量图像生成

2024-05-08 00:50:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要构建一个以两个潜在变量的值为输入的深度神经网络,并生成一个灰度图像。你知道吗

我知道这类似于GANs中的生成器网络,但是否有任何已发表的研究成果或任何Python/Tensorflow/Keras代码专门用于此类学习任务?你知道吗


Tags: 代码图像网络tensorflow神经网络灰度kerasgans
1条回答
网友
1楼 · 发布于 2024-05-08 00:50:17

所以这可能是一个任务,但不一定,取决于你手头的数据。但现在来了

Code for the toyproblem of generating MNIST samples with GANs:

# define variables
g_input_shape = 100 
d_input_shape = (28, 28) 
hidden_1_num_units = 500 
hidden_2_num_units = 500 
g_output_num_units = 784 
d_output_num_units = 1 
epochs = 25 
batch_size = 128

# generator
model_1 = Sequential([
    Dense(units=hidden_1_num_units, input_dim=g_input_shape, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)),
    Dense(units=hidden_2_num_units, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)),   
    Dense(units=g_output_num_units, activation='sigmoid', kernel_regularizer=L1L2(1e-5, 1e-5)),
    Reshape(d_input_shape),
])

# discriminator
model_2 = Sequential([
    InputLayer(input_shape=d_input_shape),
    Flatten(),   
    Dense(units=hidden_1_num_units, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)),
    Dense(units=hidden_2_num_units, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)),    
    Dense(units=d_output_num_units, activation='sigmoid', kernel_regularizer=L1L2(1e-5, 1e-5)),
])


from keras_adversarial import AdversarialModel, simple_gan, gan_targets
from keras_adversarial import AdversarialOptimizerSimultaneous, normal_latent_sampling

# Let us compile our GAN and start the training
gan = simple_gan(model_1, model_2, normal_latent_sampling((100,)))
model = AdversarialModel(base_model=gan,player_params=[model_1.trainable_weights, model_2.trainable_weights])
model.adversarial_compile(adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy')

history = model.fit(x=train_x, y=gan_targets(train_x.shape[0]), epochs=10, batch_size=batch_size)

# We get a graph like after training for 10 epochs.
plt.plot(history.history['player_0_loss'])
plt.plot(history.history['player_1_loss'])
plt.plot(history.history['loss'])

# After training for 100 epochs, we can now generate images
zsamples = np.random.normal(size=(10, 100))
pred = model_1.predict(zsamples)
for i in range(pred.shape[0]):
    plt.imshow(pred[i, :], cmap='gray')
plt.show()

即使在这件事上弄脏了你的手,你真的应该从reading开始研究围绕着GANs和它们的适应发展。你知道吗

Note:

当你有一把漂亮的锤子时,很容易把你所有的任务都当成钉子。

但它不一定漂亮。如果你能提供更多关于你的问题的细节,回答你的问题也会容易得多。你知道吗

  1. 潜变量是什么样子的?你知道吗
  2. 它们是否与灰度图像配对?你知道吗
  3. 你有多少数据?规格是什么?你知道吗

相关问题 更多 >