A.I Latest

GAN supposed to produce hand written digits creates lines instead


I am reasearching a project with this code of a GAN model used to produce handwritten digits with the mnist dataset all was when I first trained the AI model it produced an okay result but I wanted to see one better so I saved the model using g_model.save("g_model_50.h5") and trained it again hoping to get a better result but when I outputted an image is showed me a bunch of lines that look nothing like the previous result I then saved this model with g_model.save("g_model_100.h5")


enter image description here

here is the code:

from numpy import expand_dims
from numpy import zeros
from numpy import ones
from numpy import vstack
import tensorflow as tf
from numpy.random import randn
from numpy.random import randint
from keras.datasets.mnist import load_data
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
from keras.layers import Reshape
from keras.layers import Flatten
from keras.layers import Conv2D
from keras.layers import Conv2DTranspose
from keras.layers import LeakyReLU
from keras.layers import Dropout
from matplotlib import pyplot
import numpy as np

def discriminator(in_shape=(28,28,1)):
    model = Sequential()
    model.add(Conv2D(64, (3,3), strides=(2, 2), padding='same', input_shape=in_shape))
    model.add(Conv2D(64, (3,3), strides=(2, 2), padding='same'))
    model.add(Dense(1, activation='sigmoid'))
    opt = Adam(lr=0.0002, beta_1=0.5)
    model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
    return model

def generator(latent_dim):
    model = Sequential()
    n_nodes = 128 * 7 * 7
    model.add(Dense(n_nodes, input_dim=latent_dim))
    model.add(Reshape((7, 7, 128)))
    model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
    model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
    model.add(Conv2D(1, (7,7), activation='sigmoid', padding='same'))
    return model

def gan(g_model, d_model):
    d_model.trainable = False
    model = Sequential()
    opt = Adam(lr=0.0002, beta_1=0.5)
    model.compile(loss='binary_crossentropy', optimizer=opt)
    return model

def load_real_samples():
    (trainX, _), (_, _) = load_data()
    X = expand_dims(trainX, axis=-1)
    X = X.astype('float32')
    X = X / 255.0
    return X

def generate_real_samples(dataset, n_samples):
    ix = randint(0, dataset.shape[0], n_samples)
    X = dataset[ix]
    y = ones((n_samples, 1))
    return X, y

def generate_latent_points(latent_dim, n_samples):
    x_input = randn(latent_dim * n_samples)
    x_input = x_input.reshape(n_samples, latent_dim)
    return x_input

def generate_fake_samples(g_model, latent_dim, n_samples):
    x_input = generate_latent_points(latent_dim, n_samples)
    X = g_model.predict(x_input)
    y = zeros((n_samples, 1))
    return X, y

def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=50, n_batch=256):
    bat_per_epo = int(dataset.shape[0] / n_batch)
    half_batch = int(n_batch / 2)
    for i in range(n_epochs):
        for j in range(bat_per_epo):
            X_real, y_real = generate_real_samples(dataset, half_batch)
            X_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
            X, y = vstack((X_real, X_fake)), vstack((y_real, y_fake))
            d_loss, _ = d_model.train_on_batch(X, y)
            X_gan = generate_latent_points(latent_dim, n_batch)
            y_gan = ones((n_batch, 1))
            g_loss = gan_model.train_on_batch(X_gan, y_gan)
            print('>%d, %d/%d, d=%.3f, g=%.3f' % (i+1, j+1, bat_per_epo, d_loss, g_loss))
# size of the latent space
latent_dim = 100
# create the discriminator
d_model = discriminator()
# create the generator
g_model = generator(latent_dim)
# create the gan
gan_model = gan(g_model, d_model)
# load image data
dataset = load_real_samples()
model = load_model("g_model_100.h5")

# train model
train(model, d_model, gan_model, dataset, latent_dim)
model = load_model("g_model_100.h5")

X, y = generate_fake_samples(model, latent_dim, 1)
X = np.squeeze(X, axis=-1)
import matplotlib.pyplot as plt
X = X.reshape((28, 28))

For feedback or comments, reach us on hello@newswire.ae



About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

A.I Latest

Looking for best option to export data into achieved then use the data to search for best match based on the certain parameters

We have an old way of storing the archive data in SQL Server and have an algorithm to retrieve the