这里直接展示我自己编写的GAN代码
关于GAN的原理介绍这里就不多讲了~~
import os
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
import numpy as np
import pandas as pd
import glob
from tensorflow import keras
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
batch_size = 64
buffer_size = 60000
# 导入图像
(train_image,train_label),(_,_) = tf.keras.datasets.fashion_mnist.load_data()
train_image = train_image.reshape(train_image.shape[0],28,28,1).astype('float32') # 将二维 图像 转换为 三维 图像
train_image = train_image/255
datasets = tf.data.Dataset.from_tensor_slices(train_image)
datasets = datasets.shuffle(buffer_size).batch(batch_size)
# 生成器构建, 这里主要采用了反卷积的操作
def generator_model():
model = tf.keras.Sequential()
model.add( tf.keras.layers.Dense(512*3*3,input_shape =(256 ,),use_bias = False)) # input_shape 是 自由定义的
model.add( tf.keras.layers.Reshape((3, 3, 512)))
model.add( tf.keras.layers.LeakyReLU())
model.add( tf.keras.layers.Conv2DTranspose<