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.
This repository collects two small, runnable notebooks that demonstrate two common building blocks of a modern generative-image workflow:
- Generate an image from a text prompt with a Stable Diffusion pipeline.
- 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.
- 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
.ipynbfiles)
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, andPillow(PIL) for displaying the image
Open either notebook in Jupyter and run the cells top to bottom. The first cell in each notebook installs the required packages.
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.
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.
The ImageReward_score.ipynb notebook uses the following bundled sample image (elephant.png, a 512×512 generated illustration):
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.
These notebooks download pretrained models from the Hugging Face Hub at runtime; no local datasets are required.
- Stable Diffusion checkpoint:
sd-dreambooth-library/mr-potato-head - Preference model:
ImageReward-v1.0(loaded via theimage-rewardpackage)
These notebooks are usage demos built on top of existing open-source projects; they are not a re-implementation of any paper.
- Hugging Face Diffusers — used for the Stable Diffusion pipeline: https://github.com/huggingface/diffusers
- ImageReward — used for image–prompt alignment scoring: https://github.com/THUDM/ImageReward
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}
}This repository is licensed under the MIT License. See the LICENSE file for details.
