Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgan with custom images from gasf several issues with expected input #8

Open
Goitsemedi888 opened this issue Apr 22, 2024 · 0 comments

Comments

@Goitsemedi888
Copy link

i found your video's on youtube and decided to model my project after your work. I am a novice at computer science

import tensorflow as tf

Define the parameters

batch_size = 32
img_height = 128
img_width = 128

Function to preprocess images (convert to grayscale and normalize)

def preprocess_images(image, label):
# Convert RGB image to grayscale
#image = tf.image.rgb_to_grayscale(image)
# Normalize pixel values to [0, 1]
image = tf.cast(image, tf.float32) / 255.0
return image, label

Load the training data

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
'/content/drive/MyDrive/gasf_images_cgan/',
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)

Load the validation data

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
'/content/drive/MyDrive/gasf_images_cgan/',
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)

train_ds = train_ds.map(preprocess_images)
val_ds = val_ds.map(preprocess_images)

Normalize pixel values to [0, 1]

normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)

Preprocess the dataset: convert to float32, normalize, and resize

train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
val_ds = val_ds.map(lambda x, y: (normalization_layer(x), y))

Additional preprocessing: convert to grayscale and normalize

train_ds = train_ds.map(preprocess_images)
val_ds = val_ds.map(preprocess_images)

Use cache(), shuffle(), batch(), and prefetch() operations

#train_ds = train_ds.cache().shuffle(1000).batch(batch_size).prefetch(buffer_size=tf.data.AUTOTUNE)
#val_ds = val_ds.cache().prefetch(buffer_size=tf.data.AUTOTUNE)

Verify the shape of the dataset

for images, labels in train_ds.take(1):
print("Shape of image batch: ", images.shape)
print("Shape of label batch: ", labels.shape)

Found 6758 files belonging to 12 classes.
Using 5407 files for training.
Found 6758 files belonging to 12 classes.
Using 1351 files for validation.
Shape of image batch: (32, 128, 128, 3)
Shape of label batch: (32,)

import os
import tensorflow as tf
from tensorflow.keras import layers, models, optimizers
from tensorflow.keras.callbacks import Callback
from tensorflow.keras.preprocessing.image import array_to_img

def build_generator():
# Input noise
noise = layers.Input(shape=(128,))
# Conditioning label
label = layers.Input(shape=(1,))

# Embed the label and reshape to match noise dimensions
label_embedding = layers.Embedding(input_dim=10, output_dim=128)(label)
label_embedding = layers.Flatten()(label_embedding)

# Concatenate noise and label embedding as generator input
model_input = layers.Concatenate()([noise, label_embedding])

x = layers.Dense(64 * 64 * 128, use_bias=False)(model_input)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)

x = layers.Reshape((64, 64, 128))(x)  # Reshape to desired image dimensions

x = layers.Conv2DTranspose(64, (5, 5), strides=(1, 1), padding='same', use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)

x = layers.Conv2DTranspose(3, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh')(x)

model = models.Model([noise, label], x)
return model

Define the discriminator model

def build_discriminator():
model = models.Sequential()
model.add(layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same', input_shape=(128, 128, 3)))
model.add(layers.LeakyReLU())
model.add(layers.Dropout(0.3))

model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'))
model.add(layers.LeakyReLU())
model.add(layers.Dropout(0.3))

model.add(layers.Flatten())
model.add(layers.Dense(1))

return model

Define the RT_IOTcGAN model

Define the RT_IOTcGAN model

class RT_IOTcGAN(tf.keras.Model):
def init(self, generator, discriminator):
super(RT_IOTcGAN, self).init()
self.generator = generator
self.discriminator = discriminator

def compile(self, g_opt, d_opt, g_loss, d_loss):
    super(RT_IOTcGAN, self).compile()
    self.g_opt = g_opt
    self.d_opt = d_opt
    self.g_loss = g_loss
    self.d_loss = d_loss

def train_step(self, real_images):
    # Unpack the batched real_images
    real_images, _ = real_images  # Assuming real_images is a tuple (images, labels)
    batch_size = tf.shape(real_images)[0]

    with tf.GradientTape() as d_tape:
        noise = tf.random.normal((batch_size, 128))
        fake_images = self.generator(noise, training=True)

        real_output = self.discriminator(real_images, training=True)
        fake_output = self.discriminator(fake_images, training=True)

        d_loss_real = self.d_loss(tf.ones_like(real_output), real_output)
        d_loss_fake = self.d_loss(tf.zeros_like(fake_output), fake_output)
        total_d_loss = d_loss_real + d_loss_fake

    d_grads = d_tape.gradient(total_d_loss, self.discriminator.trainable_weights)
    self.d_opt.apply_gradients(zip(d_grads, self.discriminator.trainable_weights))

    with tf.GradientTape() as g_tape:
        noise = tf.random.normal((batch_size, 128))
        fake_images = self.generator(noise, training=True)
        fake_output = self.discriminator(fake_images, training=True)

        g_loss = self.g_loss(tf.ones_like(fake_output), fake_output)

    g_grads = g_tape.gradient(g_loss, self.generator.trainable_weights)
    self.g_opt.apply_gradients(zip(g_grads, self.generator.trainable_weights))

    return {"d_loss": total_d_loss, "g_loss": g_loss}

Assuming build_generator() and build_discriminator() functions are defined elsewhere

Create an instance of the generator and discriminator

generator = build_generator()
discriminator = build_discriminator()

Create an instance of RT_IOTcGAN

rtiotcgan = RT_IOTcGAN(generator, discriminator)

Compile the model

g_opt = tf.keras.optimizers.Adam(learning_rate=0.0001)
d_opt = tf.keras.optimizers.Adam(learning_rate=0.00001)
g_loss = tf.keras.losses.BinaryCrossentropy()
d_loss = tf.keras.losses.BinaryCrossentropy()

rtiotcgan.compile(g_opt, d_opt, g_loss, d_loss)

Define the ModelMonitor callback

class ModelMonitor(Callback):
def init(self, num_img=3, latent_dim=128):
self.num_img = num_img
self.latent_dim = latent_dim

def on_epoch_end(self, epoch, logs=None):
    random_latent_vectors = tf.random.normal((self.num_img, self.latent_dim))
    generated_images = self.model.generator(random_latent_vectors, training=False)
    generated_images *= 255
    generated_images = generated_images.numpy()
    for i in range(self.num_img):
        img = array_to_img(generated_images[i])
        img.save(os.path.join('images', f'generated_img_epoch_{epoch}_sample_{i}.png'))

Create an instance of ModelMonitor callback

model_monitor = ModelMonitor(num_img=3, latent_dim=128)

Train the model

hist = rtiotcgan.fit(train_ds, epochs=200, callbacks=[model_monitor])

the code never runs several errors about expected input. i would appreciate all the help i can get. please and thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant