You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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,))
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))
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
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
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
The text was updated successfully, but these errors were encountered: