King-Of-Knights / Keras-ACGAN-CIFAR10

48 stars 19 forks source link

image extraction #5

Open yrg23 opened 5 years ago

yrg23 commented 5 years ago

thank for your awesome work, after whole training complete how can i generate examples and save them one by one. thanks...

King-Of-Knights commented 5 years ago

Hello! The function of generator will map latent variable into fake image. So, all you need to do is:

from keras.models import load_model
import matplotlib.pyplot as plt
import numpy as np

def vis_square(data, padsize=1, padval=0):

            # force the number of filters to be square
            n = int(np.ceil(np.sqrt(data.shape[0])))
            padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
            data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))

            # tile the filters into an image
            data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
            data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
            return data

noise = np.random.normal(0, 0.5, (1, 110))
gen = load_model('generator.hdf5') # Modified to your generator hdf5 file
noise = np.random.normal(0, 0.5, (100, latent_size))
sampled_labels = np.array([[i] * 10 for i in range(10)]).reshape(-1, 1)
generated_images = generator.predict([noise, sampled_labels]).transpose(0, 2, 3, 1) 
generated_images = np.asarray((generated_images * 127.5 + 127.5).astype(np.uint8))
img = vis_square(generated_images)
plt.imshow(img)
plt.savefig('generator_image.png')
plt.show() # if you wish to see them

then you set a loop if you want to generate more! I hope this works for u! :)

yrg23 commented 5 years ago

Awesome, thanks for your quick response. I have another question but its a general question, actually. I have a data set contains 5 classes (each has about 50 samples). I want to generate GAN samples. I decided to use ACGAN in order to take advantage of class information rather than DCGAN etc. So, can you give me more tips how can i build a stable model. Can classic data augmentation techniques help to improve training, do u think?

King-Of-Knights commented 5 years ago

@yrg23 As far as I know, the dataset‘s Statistics information play an importance role in your task. Some simple dataset like human face and MNIST which has strong Statistics feature will make it easier for generator to learn the Probability distribution and fool the discriminator. Some hard dataset like Cifar10 and ImageNet will prevent generator from learning Probability distribution easily and discriminator will easily discriminate the real and fake image. This will lead model train fail. In a word, I believe your dataset attribution play desicieve factor and you can have a try on your dataset first, see if it could produce awesome image. Good luck!

yrg23 commented 5 years ago

thanks. i will try...

yrg23 commented 5 years ago

sorry to bother you again. one last question. my data set consists of 5 classes as i mentioned before. if i want to produce images for five classes only (not 10), which part of your code i should change? i tried but i get dimension error.

King-Of-Knights commented 5 years ago

change both '10' to '5' in sampled_labels, you will get 25×25 for your 5 classes image

yrg23 commented 5 years ago

thanks but why the final image dimension does not reflect the generated images' dimensions. if i want to produce an image consists of 10 in row and 10 in column, so, the final image should be in dimension of 32x10=(320 x 320), right? it should be related with vis_square function but, i could not figure it out yet

King-Of-Knights commented 5 years ago

In vis_square function, there exists black line between two figures. You need to take that into consideration

yrg23 commented 5 years ago

i noticed now. thank again