10. Demonstration of GAN with image output
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
# Generator model
generator = keras.Sequential([
layers.Dense(256, input_dim=100),
layers.LeakyReLU(alpha=0.2),
layers.Dense(512),
layers.LeakyReLU(alpha=0.2),
layers.Dense(784, activation='tanh'),
layers.Reshape((28, 28, 1))
])
# Generate random images
def generate_images(num_images):
noise = np.random.normal(0, .1, (num_images, 100))
generated_images = generator.predict(noise)
generated_images = 0.5 * generated_images + 0.5
return generated_images
# Generate and display images
num_images = 25
generated_images = generate_images(num_images)
fig, axs = plt.subplots(5, 5)
cnt = 0
for i in range(5):
for j in range(5):
axs[i, j].imshow(generated_images[cnt, :, :, 0], cmap='magma')
axs[i, j].axis('off')
cnt += 1
plt.show()
------------------------------------------------------------------------
In addition to 'gray', which is commonly used for grayscale images, matplotlib provides several other colormaps (cmaps) that you can use to display images. Here are some commonly used colormaps:
Sequential colormaps:
'viridis': A perceptually uniform colormap ranging from blue to yellow.'plasma': A colormap that goes from dark purple to bright yellow.'inferno': A colormap with black, red, and yellow colors.'magma': A colormap that goes from black to white through purple and pink.'cividis': A colormap designed to be easily interpreted by individuals with colorblindness.
Diverging colormaps:
'coolwarm': A colormap that transitions from cool colors (blues) to warm colors (reds).'bwr': A blue-white-red diverging colormap.'RdBu': A red-white-blue diverging colormap.
Qualitative colormaps:
'tab10': A colormap with 10 distinct colors.'tab20': A colormap with 20 distinct colors.
Miscellaneous colormaps:
'jet': A popular colormap that goes from blue to green to red to yellow.'rainbow': A colormap that spans the entire visible spectrum.'hot': A colormap ranging from black to red and white.'gray_r': The reverse grayscale colormap.'cool': A colormap that transitions from cyan to purple.
These are just a few examples, and there are many more colormaps available in matplotlib.
Comments
Post a Comment