Dataset Viewer
Auto-converted to Parquet Duplicate
Search is not available for this dataset
image
image

YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

First-Frame Generation with Qwen-Image-Edit-2509 + SAM2

Generate realistic first-frame images for video data pipelines by editing a source hand image into diverse medical, lab, kitchen, and robotic scenes — with optional SAM2 masking to preserve the original hand pose pixel-exactly.


Overview

This pipeline produces edited first-frame still images from a real hand photograph using two models:

Model Role
Qwen-Image-Edit-2509 (7B) Image-to-image editing via flow matching diffusion
SAM2.1 hiera-small Hand segmentation for mask-based compositing

Two output modes:

  • Raw edit — Qwen rewrites the full image (background + hand + object)
  • Composited edit — Qwen's background/object kept; original hand pixels pasted back using SAM2 mask

GPU Requirements

Component VRAM needed
Qwen-Image-Edit-2509 (bfloat16 + CPU offload) ~10 GB
SAM2.1 hiera-small ~0.5 GB
Both together (sequential) ~10 GB
  • Minimum: 1× GPU with 10 GB VRAM (e.g. RTX 3080, A10, A100)
  • Recommended: 1× A100 40 GB or H100 for comfortable headroom
  • CPU offload (enable_model_cpu_offload()) is enabled by default — this keeps peak VRAM usage low by moving model layers to RAM between inference steps
  • Generation time: ~90 seconds per image on an A100 40 GB

Setup on a New Machine

1. Clone / copy the repo

git clone <your-repo-url>
cd scale_up_video_data_gen

2. Create a Python virtual environment

Python 3.10 or 3.11 recommended (tested on 3.10).

python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip

3. Install dependencies

# Core diffusers stack
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
pip install diffusers transformers accelerate

# Image utilities
pip install opencv-python-headless pillow numpy

# SAM2 (for compositing pass)
pip install git+https://github.com/facebookresearch/sam2.git

Note: If your cluster driver supports CUDA 12.2+, use cu121. For older drivers use cu118.

4. Download model weights

Qwen-Image-Edit-2509

mkdir -p models
huggingface-cli download Qwen/Qwen2.5-VL-7B-Instruct-GPTQ-Int4 \
    --local-dir models/Qwen-Image-Edit-2509

Or manually place the model under models/Qwen-Image-Edit-2509/.

SAM2.1 hiera-small

mkdir -p models/sam2.1
wget -O models/sam2.1/sam2.1_hiera_small.pt \
    https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_small.pt

5. Prepare source image

Place your source hand image under source_image/. Two example images are provided:

File Description
source_image/corl.jpg Hand on desk with laptop — rich scene context, best editing results
source_image/corl_human_hand.jpg Open palm on white background — good for full scene replacement

Running the Pipeline

Pass A — Qwen image editing (generates *_raw.png)

source .venv/bin/activate
python gen_corl_hand_scenes.py      # for corl_human_hand.jpg source
# or
python gen_hand_edits.py            # for corl.jpg source

This generates one edited image per prompt (~90s each). Raw outputs are saved as *_raw.png.

Pass B — SAM2 compositing (generates final *.png)

python gen_corl_hand_composite.py   # for corl_human_hand.jpg
# or
python gen_hand_recomposite.py      # for corl.jpg

This runs SAM2 on each raw edit, locates the generated hand, and replaces it with the original hand pixels from the source image.

Important: Run Pass A and Pass B as separate processespipeline/vace_pkgs conflicts with diffusers torch internals if both are loaded in the same Python session.

SLURM (cluster) submission

sbatch -A <your_account> -p interactive --gres=gpu:1 \
  --output=slurm_scenes.log \
  --wrap="source .venv/bin/activate && cd scale_up_video_data_gen && \
          python gen_corl_hand_scenes.py && python gen_corl_hand_composite.py"

How the Generation Works

Qwen-Image-Edit-2509

Qwen-Image-Edit-2509 is a flow-matching diffusion model that takes a source image + text prompt and produces an edited image. Internally:

  1. VAE encode — source image → latent tensor
  2. DiT denoise — 40-step flow matching denoising conditioned on the text prompt and source latent
  3. VAE decode — latent → edited RGB image

Key parameters used:

qpipe(
    image=[src_pil],           # source PIL image, resized to (896, 672)
    prompt=prompt,             # scene/object description
    negative_prompt=" ",       # minimal negative prompt
    true_cfg_scale=4.0,        # classifier-free guidance strength
    num_inference_steps=40,    # denoising steps
    generator=torch.Generator(device="cpu").manual_seed(42),
)
  • true_cfg_scale=4.0 — higher values follow the prompt more strictly but reduce diversity
  • Input is resized to (896, 672) (landscape) or (672, 896) (portrait) to match the model's preferred aspect ratio

SAM2 Compositing

To preserve the original hand pose exactly:

  1. Run SAM2ImagePredictor on the Qwen output with a single point prompt at the center of the hand
  2. Select the best mask from 3 candidates (multimask_output=True)
  3. Apply a 31-pixel Gaussian feather to soften the mask boundary
  4. Composite:
    output = edit_pixels × (1 − mask) + source_pixels × mask
    
    — i.e. keep Qwen's scene/object, but replace the hand region with the original hand

Output Structure

source_image/
├── corl.jpg                        ← source (desk scene)
├── corl_human_hand.jpg             ← source (open palm)
│
├── hand_edits/                     ← Pass A only, from corl.jpg
│   ├── 01_surgical_knife.png
│   └── ...
│
├── hand_edits_masked/              ← Pass A + B, from corl.jpg
│   ├── 01_surgical_knife.png       ← final composite
│   ├── 01_surgical_knife_raw.png   ← Qwen raw output
│   ├── 01_surgical_knife_qwen_mask.png  ← SAM2 mask on Qwen output
│   ├── hand_mask.png               ← SAM2 mask on source image
│   ├── hand_mask_overlay.jpg       ← visual overlay for inspection
│   └── ...
│
└── corl_hand_scenes/               ← Pass A + B, from corl_human_hand.jpg
    ├── 01_surgical_room.png
    ├── 01_surgical_room_raw.png
    └── ...

Prompts Used

From corl.jpg (desk scene) — hand_edits_masked/

File Prompt summary
01_surgical_knife Left hand holding a stainless steel surgical scalpel, same desk background
02_apple Left hand holding a fresh red apple, same desk background
03_syringe Left hand holding a medical syringe, same desk background
04_pen Left hand holding a black ballpoint pen, same desk background
05_smartphone Left hand holding a smartphone screen-up, same desk background
06_stethoscope Left hand holding a stethoscope chest piece, same desk background
07_scissors Left hand holding surgical scissors, same desk background
08_coffee_cup Left hand wrapping a white ceramic coffee cup, same desk background
09_pill_bottle Left hand holding an orange pill bottle, same desk background
10_gloved_scalpel Left hand in blue nitrile glove holding scalpel, OR room background

From corl_human_hand.jpg (open palm) — corl_hand_scenes/

File Prompt summary
01_surgical_room Hand in blue nitrile glove over green surgical drape, OR lighting
02_scalpel Hand gripping scalpel over blue sterile drape
03_syringe Hand holding syringe with clear liquid
04_stethoscope Hand holding stethoscope, white coat, hospital corridor
05_pill_bottle Hand holding orange pill bottle
06_test_tube Hand holding glowing blue test tube, lab background
07_tablet_medical Hand holding medical tablet with X-ray on screen
08_robot_lab Hand reaching toward silver robotic arm in robotics lab
09_chef_knife Hand gripping chef's knife over cutting board, kitchen
10_iv_drip Hand with IV line on hospital bed

Tips

  • Best results come from source images with an existing scene (desk, table, room) rather than plain white backgrounds — Qwen works as an editor, not a generator from scratch
  • If you see two hands in the output, it means the compositing mask didn't cover Qwen's generated hand — check the *_qwen_mask.png files to diagnose
  • For background-change scenes (e.g. white→OR room), skip Pass B and use the raw Qwen output directly — compositing will mismatch the backgrounds
  • SAM2 point prompt should be placed at the center of the palm in the edited image coordinate space

Dependencies Summary

torch>=2.1
torchvision
diffusers>=0.30
transformers>=4.40
accelerate
opencv-python-headless
pillow
numpy
sam2 (from facebookresearch/sam2)
Downloads last month
96