Introduction
Image generation helps computers create new pictures from patterns they learn. It is useful for art, design, and fun projects.
import tensorflow as tf from tensorflow.keras import layers # Define a simple generator model model = tf.keras.Sequential([ layers.Dense(7*7*128, use_bias=False, input_shape=(100,)), layers.BatchNormalization(), layers.LeakyReLU(), layers.Reshape((7, 7, 128)), layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False), layers.BatchNormalization(), layers.LeakyReLU(), layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh') ])
noise = tf.random.normal([1, 100]) generated_image = model(noise, training=False)
model.summary()
import tensorflow as tf from tensorflow.keras import layers import matplotlib.pyplot as plt # Define the generator model model = tf.keras.Sequential([ layers.Dense(7*7*128, use_bias=False, input_shape=(100,)), layers.BatchNormalization(), layers.LeakyReLU(), layers.Reshape((7, 7, 128)), layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False), layers.BatchNormalization(), layers.LeakyReLU(), layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh') ]) # Generate one image from random noise noise = tf.random.normal([1, 100]) generated_image = model(noise, training=False) # Convert the output to a displayable image import numpy as np image = generated_image[0, :, :, 0].numpy() image = (image + 1) / 2 # Scale from [-1,1] to [0,1] # Show the image plt.imshow(image, cmap='gray') plt.axis('off') plt.show() print('Generated image shape:', generated_image.shape)
noise = tf.random.normal([1, 100]) generated_image = generator(noise, training=False)
noise = tf.random.normal([100]) generated_image = generator(noise, training=False)
Pass the test and this pattern moves unknown