Image-to-image transformation in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate
import tensorflow as tf from tensorflow.keras import layers, models from tensorflow.keras.preprocessing.image import ImageDataGenerator # Define data augmentation train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, validation_split=0.2 ) # Load data (placeholder, replace with actual data loading) # X_train, y_train = load_grayscale_and_color_images() # Create model with dropout model = models.Sequential([ layers.Input(shape=(64, 64, 1)), layers.Conv2D(64, (3,3), activation='relu', padding='same'), layers.Dropout(0.3), layers.Conv2D(64, (3,3), activation='relu', padding='same'), layers.MaxPooling2D((2,2)), layers.Conv2D(128, (3,3), activation='relu', padding='same'), layers.Dropout(0.3), layers.UpSampling2D((2,2)), layers.Conv2D(3, (3,3), activation='sigmoid', padding='same') ]) model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005), loss='mse', metrics=['mae']) # Use early stopping early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) # Fit model with data augmentation # history = model.fit( # train_datagen.flow(X_train, y_train, subset='training', batch_size=32), # validation_data=train_datagen.flow(X_train, y_train, subset='validation'), # epochs=50, # callbacks=[early_stop] # ) # For demonstration, assume after training: new_metrics = 'Training loss: 0.05, Validation loss: 0.08, Training MAE: 0.12, Validation MAE: 0.18'
Before: Training MAE 0.02, Validation MAE 0.35, high overfitting.
After: Training MAE 0.12, Validation MAE 0.18, better generalization.
Practice
What is the main goal of image-to-image transformation in AI?
Solution
Step 1: Understand the purpose of image-to-image transformation
This technique changes one image into another, like coloring or style transfer.Step 2: Compare with other image tasks
Classification, detection, and text generation are different tasks, not image transformation.Final Answer:
To change an input image into a different output image automatically -> Option AQuick Check:
Image-to-image transformation = change image [OK]
- Confusing transformation with classification
- Thinking it detects objects instead of changing images
- Mixing it up with text generation from images
Which of the following is the correct way to describe an image-to-image model's input and output?
Input: ?Output: ?
Solution
Step 1: Identify input type for image-to-image models
These models take an image as input to transform it.Step 2: Identify output type for image-to-image models
The output is also an image, changed in style, color, or content.Final Answer:
Input: Image, Output: Image -> Option AQuick Check:
Input and output both images [OK]
- Confusing input as text or numbers
- Thinking output is text instead of image
- Mixing input/output types
Consider this simplified Python code using a model for image-to-image transformation:
input_image = load_image('sketch.png')
output_image = model.transform(input_image)
save_image(output_image, 'colorized.png')
print(type(output_image))What will be printed?
Solution
Step 1: Understand typical output type of image-to-image models
Most models output images as numpy arrays representing pixel data.Step 2: Check code for output type
Since model.transform returns an image, it is usually a numpy.ndarray, not a PIL Image or string.Final Answer:
<class 'numpy.ndarray'> -> Option BQuick Check:
Model output image = numpy array [OK]
- Assuming output is a string filename
- Confusing PIL Image with numpy array
- Expecting error without context
Look at this code snippet for image-to-image transformation:
def transform_image(model, img_path):
img = load_image(img_path)
result = model.transform(img)
return result
output = transform_image(my_model, 12345)
print(type(output))What is the main error here?
Solution
Step 1: Check the argument passed to load_image
load_image expects a file path string, but 12345 is a number, causing an error.Step 2: Verify other code parts
model.transform and print syntax are correct; function returns result properly.Final Answer:
The image path should be a string, not a number -> Option CQuick Check:
Image path must be string [OK]
- Thinking model.transform is missing
- Ignoring argument type for image path
- Confusing print syntax in Python 3
You want to build an image-to-image model that converts black-and-white sketches into colored images. Which approach is best?
A dataset has pairs of sketches and their colored versions.
Solution
Step 1: Identify the task type
Converting sketches to colored images is a paired image-to-image translation task.Step 2: Choose the right training method
Supervised learning with paired data (sketch and color image) is best to learn direct mapping.Step 3: Evaluate other options
Unsupervised clustering, text-to-image, and classification do not fit this paired transformation task.Final Answer:
Train a supervised model using paired sketch and color images -> Option DQuick Check:
Paired data needs supervised training [OK]
- Choosing unsupervised methods without paired data
- Confusing text-to-image with image-to-image
- Using classification instead of transformation
Ready to prove it?
Pass the test and this pattern moves unknown
