Visualization Methods for Research

1. Training Loss Curve

To visualize the training loss over epochs, you can use the following code:

import matplotlib.pyplot as plt

# Assume loss_values is a list of loss values recorded at each epoch
loss_values = [0.9, 0.7, 0.5, 0.4, 0.35, 0.3, 0.25, 0.2]

# Plot the training loss curve
epochs = list(range(1, len(loss_values) + 1))
plt.plot(epochs, loss_values, label='Training Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training Loss over Epochs')
plt.legend()
plt.grid(True)
plt.show()

2. Comparison of Generated Images

Compare the images generated by pre-trained and optimized models using Matplotlib:

import matplotlib.pyplot as plt
from PIL import Image

# Load the images
img_pretrained = Image.open('/content/drive/My Drive/Transformer_Project/results/generated_image_pretrained.png')
img_optimized = Image.open('/content/drive/My Drive/Transformer_Project/results/generated_image_optimized.png')

# Visualization
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
ax[0].imshow(img_pretrained)
ax[0].set_title("Pre-trained Model")
ax[0].axis('off')

ax[1].imshow(img_optimized)
ax[1].set_title("Optimized Model")
ax[1].axis('off')

plt.suptitle('Comparison of Generated Images')
plt.show()

3. Distribution of Pixel Intensities

Analyze the pixel intensity distribution of generated images to understand their quality and differences:

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

# Load the images
img_pretrained = Image.open('/content/drive/My Drive/Transformer_Project/results/generated_image_pretrained.png')
img_optimized = Image.open('/content/drive/My Drive/Transformer_Project/results/generated_image_optimized.png')

# Convert images to numpy arrays
img_pretrained_np = np.array(img_pretrained)
img_optimized_np = np.array(img_optimized)

# Plot histograms of pixel intensities
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
ax[0].hist(img_pretrained_np.ravel(), bins=256, color='blue', alpha=0.7)
ax[0].set_title("Pre-trained Model Pixel Intensity")
ax[0].set_xlabel('Pixel Value')
ax[0].set_ylabel('Frequency')

ax[1].hist(img_optimized_np.ravel(), bins=256, color='green', alpha=0.7)
ax[1].set_title("Optimized Model Pixel Intensity")
ax[1].set_xlabel('Pixel Value')
ax[1].set_ylabel('Frequency')

plt.suptitle('Pixel Intensity Distribution')
plt.show()

4. Feature Map Visualization

Visualize feature maps from intermediate layers of the model to understand how the model interprets the input:

import torch
import matplotlib.pyplot as plt

# Extract feature maps from an intermediate layer
layer_name = 'text_encoder.encoder.layer.0.output.LayerNorm'
intermediate_layer = dict(model.named_modules())[layer_name]

# Register a forward hook to get the output of the intermediate layer
activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
    return hook

intermediate_layer.register_forward_hook(get_activation(layer_name))

# Input text and generate output
input_text = "A beautiful sunset over the mountains."
tokens = tokenizer(input_text, padding="max_length", truncation=True, return_tensors="pt")
with torch.no_grad():
    model(tokens)

# Visualize feature map
feature_map = activation[layer_name][0]
fig, ax = plt.subplots(1, 4, figsize=(20, 10))
for i in range(4):
    ax[i].imshow(feature_map[i].cpu().numpy(), cmap='viridis')
    ax[i].axis('off')
    ax[i].set_title(f'Feature Map {i+1}')

plt.suptitle('Feature Map Visualization from Intermediate Layer')
plt.show()