• 大小: 12.91MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-06-21
  • 语言: Python
  • 标签:

资源简介

各种对抗神经网络(GAN)大合集

资源截图

代码片段和文件信息

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib as mpl
mpl.use(‘Agg‘)
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os sys

sys.path.append(‘utils‘)
from nets import *
from datas import *

def sample_z(m n):
return np.random.uniform(-1. 1. size=[m n])

# for test
def sample_y(m n ind):
y = np.zeros([mn])
for i in range(m):
y[i i%8] = 1
#y[:7] = 1
#y[-10] = 1
return y

def concat(zy):
return tf.concat([zy]1)

class CGAN_Classifier(object):
def __init__(self generator discriminator classifier data):
self.generator = generator
self.discriminator = discriminator
self.classifier = classifier
self.data = data

# data
self.z_dim = self.data.z_dim
self.y_dim = self.data.y_dim # condition
self.size = self.data.size
self.channel = self.data.channel

self.X = tf.placeholder(tf.float32 shape=[None self.size self.size self.channel])
self.z = tf.placeholder(tf.float32 shape=[None self.z_dim])
self.y = tf.placeholder(tf.float32 shape=[None self.y_dim])

# nets
self.G_sample = self.generator(concat(self.z self.y))

self.D_real _ = self.discriminator(self.X)
self.D_fake _ = self.discriminator(self.G_sample reuse = True)

self.C_real = self.classifier(self.X)
self.C_fake = self.classifier(self.G_sample reuse = True)

# loss
self.D_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_real labels=tf.ones_like(self.D_real))) + tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_fake labels=tf.zeros_like(self.D_fake)))
self.G_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_fake labels=tf.ones_like(self.D_fake)))
self.C_real_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.C_real labels=self.y)) # real label
self.C_fake_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.C_fake labels=self.y))  

# solver
self.D_solver = tf.train.AdamOptimizer(learning_rate=2e-4 beta1=0.5).minimize(self.D_loss var_list=self.discriminator.vars)
self.G_solver = tf.train.AdamOptimizer(learning_rate=2e-4 beta1=0.5).minimize(self.G_loss + self.C_fake_loss var_list=self.generator.vars)
self.C_real_solver = tf.train.AdamOptimizer(learning_rate=2e-4 beta1=0.5).minimize(self.C_real_loss var_list=self.classifier.vars)
#self.C_fake_solver = tf.train.AdamOptimizer(learning_rate=2e-4 beta1=0.5).minimize(self.C_fake_loss var_list=self.generator.vars)

self.saver = tf.train.Saver()
gpu_options = tf.GPUOptions(allow_growth=True)
self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

def train(self sample_dir ckpt_dir=‘ckpt‘ training_epoches = 1000000 batch_size = 32):
fig_count = 0
self.sess.run(tf.global_variables_initializer())

for epoch in range(training_epoches):
# update D
for _ in range(1):
X_b y_b = self.data(batch_size)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-09-08 03:23  GAN-master\
     目录           0  2017-09-08 03:23  GAN-master\Datas\
     目录           0  2017-09-08 03:23  GAN-master\Datas\mnist\
     文件     1648877  2017-09-08 03:23  GAN-master\Datas\mnist\t10k-images-idx3-ubyte.gz
     文件        4542  2017-09-08 03:23  GAN-master\Datas\mnist\t10k-labels-idx1-ubyte.gz
     文件     9912422  2017-09-08 03:23  GAN-master\Datas\mnist\train-images-idx3-ubyte.gz
     文件       28881  2017-09-08 03:23  GAN-master\Datas\mnist\train-labels-idx1-ubyte.gz
     文件       12072  2017-09-08 03:23  GAN-master\README.md
     目录           0  2017-09-08 03:23  GAN-master\README\
     目录           0  2017-09-08 03:23  GAN-master\README\images\
     文件        7178  2017-09-08 03:23  GAN-master\README\images\cgan.png
     文件        6753  2017-09-08 03:23  GAN-master\README\images\gan.png
     文件        4228  2017-09-08 03:23  GAN-master\README\images\infogan1.png
     文件        4777  2017-09-08 03:23  GAN-master\README\images\infogan2.png
     目录           0  2017-09-08 03:23  GAN-master\README\results\
     文件       47347  2017-09-08 03:23  GAN-master\README\results\cgan_mlp.png
     文件      151303  2017-09-08 03:23  GAN-master\README\results\face3D_dcgan.png
     目录           0  2017-09-08 03:23  GAN-master\Samples\
     目录           0  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\
     文件       32085  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\000_0.png
     文件       14637  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\001_1.png
     文件       13206  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\002_2.png
     文件       12778  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\003_3.png
     文件       12493  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\004_4.png
     文件       12609  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\005_5.png
     文件       12861  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\006_6.png
     文件       11793  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_classifier\348_8.png
     目录           0  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_conv\
     文件       31917  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_conv\000_0.png
     文件       13567  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_conv\001_1.png
     文件       11557  2017-09-08 03:23  GAN-master\Samples\mnist_cgan_conv\002_2.png
............此处省略127个文件信息

评论

共有 条评论