GAN (Generative Adversarial Network,GAN)生成式对抗网络

在这里插入图片描述
GAN (Generative Adversarial Network,GAN)生成式对抗网络_第1张图片处理可忽略的报错

import warnings
warnings.filterwarnings("ignore")

克隆仓库

git clone https://gitee.com/mindspore/models.git
cp -r ~/CANN/models/research/cv/wgan ./wgan/
cp -r ~/CANN/models/research/cv/gan ./gan/

最后没仔细看,要求的是mnist即可
那我们就搞一个mnist的脚本
注意scipy.misc已经没有imsave了

def save_img(img, fname):
    pil_img = deprocess_image(np.copy(img))
    scipy.misc.imsave(fname, pil_img)

改成

def save_img(img, fname):
    pil_img = deprocess_image(np.copy(img))
    imageio.imwrite(fname, pil_img)

GAN (Generative Adversarial Network,GAN)生成式对抗网络_第2张图片
本地训练较慢,那我们只训练3轮
在云脑上,我们同步训练一个:

pip install tensorflow-gpu==1.13.1
测试GPU
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
--------
import tensorflow as tf
tf.debugging.set_log_device_placement(True)
--------
# Place tensors on the CPU
with tf.device('/GPU:0'):
  a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
  b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
--------
c = tf.matmul(a, b)
print(c)
--------
import tensorflow as tf
print (tf.__version__)
if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

wgan恢复为gan
wgan定义的:

            # gradient penalty

            gp = gradient_penalty(D, xr, xf.detach())


            # aggregate all

            loss_D = lossr + lossf + 0.2 * gp

改为

# aggregate all
            loss_D = lossr + lossf

GAN (Generative Adversarial Network,GAN)生成式对抗网络_第3张图片
如果想不输出解压过程

unzip xx.zip > /dev/null 2>&1

GAN (Generative Adversarial Network,GAN)生成式对抗网络_第4张图片
在这里插入图片描述
pytorch也可以实现类似的功能。

读取tfevents
读取的步骤:①先转到events.out.tfevents所在的文件目录下

                  命令:cd dir(所在文件夹目录)

                  ②tensorboard --logdir dir(文件所在文件夹)

                  ③firefox浏览器打开第二部生成的网址,例如http://ubuntu122:6006/ 

你可能感兴趣的:(昇腾,生成对抗网络,tensorflow,深度学习)