Skip to content

About

Jupyter notebooks for text-to-image generation with Stable Diffusion and image-prompt alignment scoring via ImageReward (Hugging Face)

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

Simple-GenAI-Notebooks

Minimal, self-contained Jupyter notebooks for text-to-image generation with Stable Diffusion (Hugging Face diffusers) and for scoring how well an image matches a text prompt using ImageReward.

Overview

This repository collects two small, runnable notebooks that demonstrate two common building blocks of a modern generative-image workflow:

  1. Generate an image from a text prompt with a Stable Diffusion pipeline.
  2. Evaluate how well an existing image aligns with one or more text prompts using the ImageReward preference model.

Each notebook is independent, installs its own dependencies in the first cell, and is meant as a quick, readable reference rather than a production framework.

Requirements

  • Python 3 (developed with Python 3.10)
  • A CUDA-capable GPU is recommended but optional — the generation notebook automatically falls back to CPU (torch.device("cuda" if torch.cuda.is_available() else "cpu")).
  • Jupyter (to open and run the .ipynb files)

There is no requirements.txt or environment.yml; each notebook installs what it needs in its first cell.

text_to_img.ipynb installs and uses:

  • torch, diffusers, transformers, accelerate, datasets, ftfy, pyarrow==9.0.0

ImageReward_score.ipynb installs and uses:

  • image-reward, clip
  • plus matplotlib, numpy, and Pillow (PIL) for displaying the image

Usage

Open either notebook in Jupyter and run the cells top to bottom. The first cell in each notebook installs the required packages.

1. Text-to-image generation — text_to_img.ipynb

Loads a Stable Diffusion pipeline and generates an image from a prompt:

model_id = "sd-dreambooth-library/mr-potato-head"
pipe = StableDiffusionPipeline.from_pretrained(model_id).to(device)

prompt = "Elephant"
image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]

To customize, edit model_id (any Stable Diffusion checkpoint on the Hugging Face Hub), the prompt string, and the sampling parameters num_inference_steps / guidance_scale. The first run downloads the model weights from the Hugging Face Hub.

2. Image–prompt alignment scoring — ImageReward_score.ipynb

Loads the ImageReward-v1.0 model and scores an image against several prompts. It uses the bundled elephant.png as the input image:

import ImageReward as RM
model = RM.load("ImageReward-v1.0")

img_path = "elephant.png"
prompts = ["elephant", "painting", "painting of an elephant", "green color"]
for prompt in prompts:
    reward = model.score(prompt, img_path)
    print(f"{prompt}: {reward}")

To score your own image, change img_path to point at your file and edit the prompts list. Higher scores indicate stronger image–prompt alignment. The first run downloads the ImageReward checkpoint.

Results

The ImageReward_score.ipynb notebook uses the following bundled sample image (elephant.png, a 512×512 generated illustration):

Sample image scored by ImageReward

Scoring this image against the four prompts produces the following ImageReward scores (as committed in the notebook output):

Prompt ImageReward score
elephant 1.0897
painting -0.8962
painting of an elephant 0.8199
green color -2.2355

The prompts that best describe the image (elephant, painting of an elephant) receive the highest scores, while an unrelated prompt (green color) receives the lowest.

Models

These notebooks download pretrained models from the Hugging Face Hub at runtime; no local datasets are required.

Credits and Citation

These notebooks are usage demos built on top of existing open-source projects; they are not a re-implementation of any paper.

If you use the ImageReward scoring in your work, please cite the original authors:

@inproceedings{xu2023imagereward,
  title     = {ImageReward: Learning and Evaluating Human Preferences for Text-to-Image Generation},
  author    = {Xu, Jiazheng and Liu, Xiao and Wu, Yuchen and Tong, Yuxuan and Li, Qinkai and Ding, Ming and Tang, Jie and Dong, Yuxiao},
  booktitle = {Advances in Neural Information Processing Systems (NeurIPS)},
  year      = {2023}
}

License

This repository is licensed under the MIT License. See the LICENSE file for details.

About

Jupyter notebooks for text-to-image generation with Stable Diffusion and image-prompt alignment scoring via ImageReward (Hugging Face)

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages