I was building a deep learning model that can generate images using a large dataset of images and a data.csv file to classify each image, on running it this error pops up
The error:
Traceback (most recent call last):
File "F:\Million Dollar files\ImageAlchemi\model.py", line 173, in <module>
gan = build_gan(generator, discriminator)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:\Million Dollar files\ImageAlchemi\model.py", line 94, in build_gan
gan_output = discriminator(generated_images)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Alam\AppData\Roaming\Python\Python311\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\Alam\AppData\Roaming\Python\Python311\site-packages\keras\src\engine\input_spec.py", line 280, in assert_input_compatibility
raise ValueError(
ValueError: Exception encountered when calling layer "discriminator" (type Functional).
Input 0 of layer "dense_1" is incompatible with the layer: expected axis -1 of input shape to have value 16384, but received input with shape (None, 4096)
Call arguments received by layer "discriminator" (type Functional):
• inputs=tf.Tensor(shape=(None, 32, 32, 3), dtype=float32)
• training=None
• mask=None
The code:
import pandas as pd
import numpy as np
import os
from PIL import Image
import tensorflow as tf
from tensorflow.keras.layers import Dense, Reshape, Flatten, Input, Conv2D, Conv2DTranspose, BatchNormalization, LeakyReLU
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
# Step 1: Import necessary libraries
# Step 2: Load and preprocess the image dataset
def load_and_preprocess_image(image_path):
img = Image.open(image_path)
img = img.resize((64, 64)) # Resize to a common size
img_array = np.array(img)
img_array = img_array / 255.0 # Normalize pixel values to [0, 1]
return img_array
def load_images_from_directory(directory):
images = []
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".png"):
img_path = os.path.join(directory, filename)
img_array = load_and_preprocess_image(img_path)
images.append(img_array)
return np.array(images)
# Step 3: Load and process the .csv file with image classifications
def load_image_classifications(csv_file):
data_df = pd.read_csv(csv_file)
return data_df['image_path'].tolist(), data_df['category'].tolist()
# Step 4: Define the GAN model architecture (generator and discriminator)
def build_generator(input_shape, initial_depth=64):
# Input layer
generator_input = Input(shape=input_shape)
# Project and reshape the input noise vector
x = Dense(initial_depth * 4 * 4, activation='relu')(generator_input)
x = Reshape((4, 4, initial_depth))(x)
# Upsampling layers using Conv2DTranspose
x = Conv2DTranspose(initial_depth * 4, (5, 5), strides=(2, 2), padding='same', activation='relu')(x)
x = Conv2DTranspose(initial_depth * 2, (5, 5), strides=(2, 2), padding='same', activation='relu')(x)
x = Conv2DTranspose(initial_depth, (5, 5), strides=(2, 2), padding='same', activation='relu')(x)
# Final output layer with tanh activation to get pixel values in [-1, 1]
generator_output = Conv2DTranspose(3, (5, 5), strides=(1, 1), padding='same', activation='tanh')(x)
# Create and compile the generator model
generator = Model(generator_input, generator_output, name='generator')
return generator
generator_input_shape = (100,) # Example: The input noise shape for the generator model
generator = build_generator(generator_input_shape)
from tensorflow.keras.layers import Input, Conv2D, Flatten, Dense, LeakyReLU
from tensorflow.keras.models import Model
def build_discriminator(input_shape, initial_depth=64):
# Input layer
discriminator_input = Input(shape=input_shape)
# Convolutional layers
x = Conv2D(initial_depth, (5, 5), strides=(2, 2), padding='same')(discriminator_input)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(initial_depth * 2, (5, 5), strides=(2, 2), padding='same')(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(initial_depth * 4, (5, 5), strides=(2, 2), padding='same')(x)
x = LeakyReLU(alpha=0.2)(x)
# Flatten layer
x = Flatten()(x)
# Final output layer with sigmoid activation to get the discriminator output
discriminator_output = Dense(1, activation='sigmoid')(x)
# Create and compile the discriminator model
discriminator = Model(discriminator_input, discriminator_output, name='discriminator')
return discriminator
discriminator_input_shape = (64, 64, 3) # Example: The input image shape for the discriminator model
discriminator = build_discriminator(discriminator_input_shape)
print("Generator Output Shape:", generator.output_shape) # It should be (None, 64, 64, 3)
print("Discriminator Input Shape:", discriminator.input_shape) # It should be (None, 64, 64, 3)
def build_gan(generator, discriminator):
# Disable training of the discriminator during GAN training
discriminator.trainable = False
# GAN input for generating fake images
gan_input = Input(shape=generator.input_shape[1:]) # Remove the batch size from the input shape
# Generate images using the generator
generated_images = generator(gan_input)
# Determine the validity of generated images using the discriminator
gan_output = discriminator(generated_images)
# Create and compile the GAN model
gan = Model(gan_input, gan_output, name='gan')
return gan
gan = build_gan(generator, discriminator)
# Step 5: Compile the GAN model
def compile_gan(gan, learning_rate=0.0002, beta_1=0.5):
gan.compile(loss='binary_crossentropy', optimizer=Adam(lr=learning_rate, beta_1=beta_1))
# Step 6: Implement the training loop for the GAN
def train_gan(generator, discriminator, gan, images, batch_size=64, epochs=10000):
# Example implementation of GAN training loop
real_labels = np.ones((batch_size, 1))
fake_labels = np.zeros((batch_size, 1))
for epoch in range(epochs):
# Select a random batch of real images
idx = np.random.randint(0, images.shape[0], batch_size)
real_images = images[idx]
# Generate a batch of fake images
noise = np.random.randn(batch_size, generator_input_shape[0])
generated_images = generator.predict(noise)
# Train the discriminator
d_loss_real = discriminator.train_on_batch(real_images, real_labels)
d_loss_fake = discriminator.train_on_batch(generated_images, fake_labels)
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
# Train the generator (via the GAN model)
g_loss = gan.train_on_batch(noise, real_labels)
# Print the progress
print(f"Epoch {epoch}/{epochs}, D Loss: {d_loss[0]}, G Loss: {g_loss}")
# Step 7: Implement a function to generate images based on user prompts
def generate_image_from_category(generator, category, image_paths, categories):
# Find an image from the specified category in the dataset
try:
indices = [i for i, cat in enumerate(categories) if cat == category]
if not indices:
raise ValueError(f"No images found for the specified category: {category}")
index = np.random.choice(indices) # Choose a random image from the specified category
image_path = image_paths[index]
img = Image.open(image_path)
img = img.resize((generator.input_shape[1], generator.input_shape[2])) # Resize to match generator input size
img_array = np.array(img)
img_array = img_array / 255.0 # Normalize pixel values to [0, 1]
# Convert to a batch of size 1 (expand_dims)
img_array = np.expand_dims(img_array, axis=0)
# Generate an image using the trained generator
generated_image = generator.predict(img_array)
# Convert the generated image to the PIL format
generated_image = np.squeeze(generated_image, axis=0)
generated_image = (generated_image * 255).astype(np.uint8)
generated_image = Image.fromarray(generated_image)
return generated_image
except ValueError as e:
print(e)
return None
# Example usage:
image_directory = 'image_dataset'
csv_file = 'data.csv'
generator_input_shape = (100,) # Example: The input noise shape for the generator model
images = load_images_from_directory(image_directory)
image_paths, categories = load_image_classifications(csv_file)
generator = build_generator(generator_input_shape)
discriminator = build_discriminator(images[0].shape)
gan = build_gan(generator, discriminator)
compile_gan(gan)
train_gan(generator, discriminator, gan, images)
# To generate images based on user prompts:
while True:
user_category = input("Enter the category for image generation (type 'exit' to quit): ")
if user_category.lower() == 'exit':
break
generate_image_from_category(generator, user_category)
Here's a summary of the code's flow:
- Import necessary libraries and functions.
- Define the functions for loading and preprocessing images from the dataset (load_and_preprocess_image and
- load_images_from_directory), loading image classifications from the CSV file (load_image_classifications), and building the GAN model architecture (build_generator, build_discriminator, and build_gan).
- Compile the GAN model using the compile_gan function.
- Implement the training loop for the GAN using the train_gan function.
- Implement a function generate_image_from_category to generate images based on user prompts.
- Load your dataset and specify the paths to the images and the CSV file.
- Build the generator, discriminator, and GAN models.
- Compile the GAN model.
- Train the GAN on your dataset using the train_gan function.
- Implement a loop to generate images based on user prompts.