对抗神经网络测试代码

对抗神经网络的报错——tf.conrrol_dependencies()

为了学习对抗神经网络,自己写了一个极为简单的对抗神经网络代码,不为生成图片,就为熟悉对抗神经网络的过程原理。代码很简单,但是错误频出,这里记录一下目前遇见最大且还没有解决的问题,如果高手光临,忘不吝赐教,真的是不胜感激。问题解决后我再更新下文章,供大家参考。

一、先描述下问题吧
程序报错:
You must feed a value for placeholder tensor ‘Placeholder_2’ with dtype float and shape [?,28,28,1]
[[Node: Placeholder_2 = Placeholderdtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/cpu:0"]]

placeholder_2是一个生成器输入的占位符,定义的时候是个二维的,一会儿详细看程序吧

二、我的环境
1、小米游戏本+deepin15.7系统
2、python3.6+tensorflow1.3-gpu版,不过配置出现了问题,好在我的程序也不大,系统提示我找不到gpu,暂改为cpu加速,意思大概是tensorflow-gpu版当成cpu版使用。
3、编译器:jupyter notebook

三、这是我的程序

mport pickle as pkl
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# 占位符
def model_placeholder(size_noise):
    # size_noise: 噪声尺寸
    learning_rate = tf.placeholder(tf.float32)
    input_noise = tf.placeholder(tf.float32, (None, size_noise))
    input_real = tf.placeholder(tf.float32, (None, 28, 28, 1))
    return (learning_rate, input_noise, input_real)

# 辨别器
def Discriminator(inputs, alpha, reuse):
    # inputs: 辨别器的输入,真实图片和生成图片 
    # alpha: leaky relu参数
    # reuse:
    with tf.variable_scope('discriminator', reuse = reuse):
        x1 = tf.layers.conv2d(inputs, filters = 64, strides = 4, kernel_size=4, padding = 'same')
        x1 = tf.maximum(alpha*x1, x1)
        
        x2 = tf.layers.conv2d(x1, filters = 128, strides = 2, kernel_size = 2, padding = 'same')
        x2 = tf.layers.batch_normalization(x2, training = True)
        x2 = tf.maximum(alpha*x2, x2)
        
        x3 = tf.reshape(x2, (-1, 128*4*4))
        logits = tf.layers.dense(x3,1)
        out = tf.sigmoid(logits)
        
        return (logits, out)
        
# 生成器
def Generator(inputs, alpha, reuse, training = True):
    # inputs: 噪声
    # alph
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于对抗神经网络的图像超分辨率算法代码的一个简单实现: ```python import tensorflow as tf import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.layers import Conv2D, Conv2DTranspose, LeakyReLU, BatchNormalization, Input, Add from tensorflow.keras.models import Model from tensorflow.keras.optimizers import Adam # 定义超分辨率生成器 def create_generator(): # 输入张量 input_img = Input(shape=(None, None, 3)) # 编码器 x = Conv2D(64, 3, padding='same')(input_img) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(64, 3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(128, 3, padding='same')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(128, 3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=0.2)(x) # 解码器 x = Conv2DTranspose(128, 3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2DTranspose(64, 3, padding='same')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2DTranspose(64, 3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=0.2)(x) output_img = Conv2DTranspose(3, 3, padding='same')(x) # 定义模型 model = Model(inputs=input_img, outputs=output_img) return model # 定义超分辨率判别器 def create_discriminator(): # 输入张量 input_img = Input(shape=(None, None, 3)) target_img = Input(shape=(None, None, 3)) # 将输入和目标图像连接在一起 x = tf.keras.layers.concatenate([input_img, target_img]) # 判别器网络结构 x = Conv2D(64, 3, padding='same')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(64, 3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(128, 3, padding='same')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(128, 3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(256, 3, padding='same')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(256, 3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(512, 3, padding='same')(x) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(512, 3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=0.2)(x) x = tf.keras.layers.Flatten()(x) x = tf.keras.layers.Dense(1)(x) # 定义模型 model = Model(inputs=[input_img, target_img], outputs=x) return model # 定义GAN模型 def create_gan(generator, discriminator): # 判别器不需要训练 discriminator.trainable = False # 输入和输出张量 input_img = Input(shape=(None, None, 3)) target_img = Input(shape=(None, None, 3)) # 生成高分辨率图像 gen_output = generator(input_img) # 判别器判断生成的高分辨率图像 gan_output = discriminator([gen_output, target_img]) # 定义GAN模型 gan_model = Model(inputs=[input_img, target_img], outputs=[gen_output, gan_output]) return gan_model # 加载数据集 def load_data(): # TODO: 加载数据集 return X_train, y_train # 训练模型 def train(): # 加载数据集 X_train, y_train = load_data() # 创建生成器和判别器 generator = create_generator() discriminator = create_discriminator() # 创建GAN模型 gan = create_gan(generator, discriminator) # 设置优化器 optimizer = Adam(lr=0.0002, beta_1=0.5) # 编译判别器 discriminator.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) # 编译GAN gan.compile(loss=['mse', 'binary_crossentropy'], optimizer=optimizer) # 训练模型 epochs = 100 batch_size = 16 steps_per_epoch = int(len(X_train)/batch_size) for epoch in range(epochs): for step in range(steps_per_epoch): # 随机选择一批数据 index = np.random.randint(0, len(X_train), batch_size) real_images = y_train[index] input_images = X_train[index] # 生成高分辨率图像 fake_images = generator.predict(input_images) # 训练判别器 real_labels = np.ones((batch_size, 1)) fake_labels = np.zeros((batch_size, 1)) d_loss_real = discriminator.train_on_batch([real_images, input_images], real_labels) d_loss_fake = discriminator.train_on_batch([fake_images, input_images], fake_labels) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) # 训练生成器 gan_labels = np.ones((batch_size, 1)) g_loss = gan.train_on_batch([input_images, real_images], [fake_images, gan_labels]) # 展示训练结果 print("Epoch:", epoch, "D Loss:", d_loss[0], "G Loss:", g_loss[0]) # 保存生成器模型 if epoch % 10 == 0: generator.save("generator.h5") # 测试模型 def test(): # 加载数据集 X_test, y_test = load_data() # 加载生成器模型 generator = tf.keras.models.load_model("generator.h5") # 随机选择一张测试图片 index = np.random.randint(0, len(X_test)) input_image = X_test[index] target_image = y_test[index] # 生成高分辨率图像 gen_output = generator.predict(input_image.reshape((1,) + input_image.shape)) # 绘制图像 fig, axes = plt.subplots(1, 3, figsize=(15, 5)) axes[0].imshow(input_image) axes[0].set_title('Input Image') axes[1].imshow(target_image) axes[1].set_title('Target Image') axes[2].imshow(gen_output[0]) axes[2].set_title('Generated Image') plt.show() # 训练模型 train() # 测试模型 test() ``` 这段代码实现了一个简单的基于对抗神经网络的图像超分辨率算法,使用了TensorFlow作为编程框架。在代码中,我们首先定义了一个超分辨率生成器和一个判别器,然后将它们作为输入传递给一个GAN模型,最后使用训练数据对GAN模型进行训练。在训练过程中,我们随机选择一批数据,生成高分辨率图像,并将它们与真实高分辨率图像一起输入到判别器中进行训练。同时,我们还将生成器训练为产生更接近真实高分辨率图像的输出。在训练完成后,我们可以使用训练好的生成器模型来生成高分辨率图像,并与真实高分辨率图像进行比较。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值