Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image extraction #5

Open
yrg23 opened this issue Feb 20, 2019 · 9 comments
Open

image extraction #5

yrg23 opened this issue Feb 20, 2019 · 9 comments

Comments

@yrg23
Copy link

yrg23 commented Feb 20, 2019

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

@King-Of-Knights
Copy link
Owner

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
Copy link
Author

yrg23 commented Feb 20, 2019

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
Copy link
Owner

@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
Copy link
Author

yrg23 commented Feb 21, 2019

thanks. i will try...

@yrg23
Copy link
Author

yrg23 commented Feb 21, 2019

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
Copy link
Owner

King-Of-Knights commented Feb 21, 2019

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

@yrg23
Copy link
Author

yrg23 commented Feb 21, 2019

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
Copy link
Owner

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

@yrg23
Copy link
Author

yrg23 commented Feb 22, 2019

i noticed now. thank again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants