-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrediction_MNIST.py
More file actions
59 lines (49 loc) · 1.78 KB
/
Copy pathPrediction_MNIST.py
File metadata and controls
59 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# #For single image example of generating an image for a specific point in the latent space
# from keras.models import load_model
# from numpy import asarray
# from matplotlib import pyplot
# from numpy.random import randn
# # load model
# model = load_model('generator_model_38k.h5')
# #To create same image, suppy same vector each time
# # all 0s
# vector = asarray([[0. for _ in range(100)]]) #Vector of all zeros
# #To create random images each time...
# vector = randn(100) #Vector of random numbers (creates a column, need to reshape)
# vector = vector.reshape(1, 100)
# # generate image
# X = model.predict(vector)
# # plot the result
# pyplot.imshow(X[0, :, :, 0], cmap='gray_r')
# pyplot.show()
# example of loading the generator model and generating images
from keras.models import load_model
from numpy.random import randn
from matplotlib import pyplot as plt
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples):
# generate points in the latent space
x_input = randn(latent_dim * n_samples)
# reshape into a batch of inputs for the network
x_input = x_input.reshape(n_samples, latent_dim)
return x_input
# create and save a plot of generated images (reversed grayscale)
def save_plot(examples, n):
# plot images
for i in range(n * n):
# define subplot
plt.subplot(n, n, 1 + i)
# turn off axis
plt.axis('off')
# plot raw pixel data
plt.imshow(examples[i, :, :, 0], cmap='gray_r')
plt.show()
# load model
model = load_model('generator_model_100K.h5')
# generate images
#Generate 16 images, each image provide a vector of size 100 as input
latent_points = generate_latent_points(100, 16)
# generate images
X = model.predict(latent_points)
# plot the result
save_plot(X, 4) #Plot 4x4 grid (Change to 5 if generating 25 images)