In [1]:
from keras.datasets import cifar10
from keras.models import load_model
from keras.utils.np_utils import to_categorical
import keras.backend as K
import numpy as np
from IPython import display
import matplotlib.pyplot as plt
% matplotlib inline
% config InlineBackend.figure_format = 'retina'
In [13]:
# Fetch data
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
# Convert uint8 pixel values to float32 in the range [0, 1] (for sigmoid)
X_test = X_test.astype('float32')
X_test /= 255
# Verify data
print X_test.shape, y_test.shape
In [15]:
for epoch in range(10, 501, 10):
discriminator = load_model("networks/disc-" + str(epoch) + ".h5")
preds = discriminator.predict(X_test)
K.clear_session() # Prevent running out of memory
accuracy = 0
for y, pred in zip(y_test, preds):
joint_pred = pred[:10] + pred[10:] # Add real and generated probabilities together
joint_pred_class = np.argmax(joint_pred)
if y == joint_pred_class:
accuracy += 1
accuracy /= 1. * len(preds)
print "Epoch {:d} test accuracy: {:.4f}".format(epoch, accuracy)
In [12]:
noise_size = 100
num_images = 50 # Must be a multiple of 10
for epoch in [250]: # range(10, 501, 10):
generator = load_model("networks/gen-" + str(epoch) + ".h5")
noise = np.random.uniform(0, 1, size=[num_images, noise_size])
noise_class = np.zeros([num_images, 10])
for image_class in range(10):
row_start = image_class * num_images / 10
row_end = (image_class + 1) * num_images / 10
noise_class[row_start:row_end, image_class] = 1
generated_images = generator.predict([noise, noise_class])
K.clear_session() # Prevent running out of memory
print "Epoch {:d} generated images:".format(epoch)
plt.figure(figsize=(10, 20))
for i in range(generated_images.shape[0]):
plt.subplot(10, num_images / 10, i+1)
img = generated_images[i,:,:,:]
plt.imshow(img)
plt.axis('off')
plt.tight_layout()
plt.show()
In [ ]: