碎碎念
现在找到另一个质量更好一些的项目和模型,并且默认不带webui,可以更好的实现代码批量化生成图像
github地址:https://github.com/VectorSpaceLab/OmniGen2
开搞
Environment Setup
✅ Recommended Setup
# 1. Clone the repo
git clone git@github.com:VectorSpaceLab/OmniGen2.git
cd OmniGen2
# 2. (Optional) Create a clean Python environment
conda create -n omnigen2 python=3.11
conda activate omnigen2
# 3. Install dependencies
# 3.1 Install PyTorch (choose correct CUDA version)
pip install torch==2.6.0 torchvision --extra-index-url https://download.pytorch.org/whl/cu124
# 3.2 Install other required packages
pip install -r requirements.txt
# Note: Version 2.7.4.post1 is specified for compatibility with CUDA 12.4.
# Feel free to use a newer version if you use CUDA 12.6 or they fixed this compatibility issue.
# OmniGen2 runs even without flash-attn, though we recommend install it for best performance.
pip install flash-attn==2.7.4.post1 --no-build-isolation🌏 For users in Mainland China
# Install PyTorch from a domestic mirror
pip install torch==2.6.0 torchvision --index-url https://mirror.sjtu.edu.cn/pytorch-wheels/cu124
# Install other dependencies from Tsinghua mirror
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# Note: Version 2.7.4.post1 is specified for compatibility with CUDA 12.4.
# Feel free to use a newer version if you use CUDA 12.6 or they fixed this compatibility issue.
# OmniGen2 runs even without flash-attn, though we recommend install it for best performance.
pip install flash-attn==2.7.4.post1 --no-build-isolation -i https://pypi.tuna.tsinghua.edu.cn/simple🧪 Run Examples
# Visual Understanding
bash example_understanding.sh
# Text-to-image generation
bash example_t2i.sh
# Instruction-guided image editing
bash example_edit.sh
# In-context generation
bash example_in_context_generation.sh提示
为了使用 OmniGen2 获得最佳效果,您可以根据具体用例调整以下关键超参数。
text_guidance_scale:控制输出遵守文本提示的严格程度(无分类器指导)。image_guidance_scale:这控制最终图像与输入参考图像的相似程度。权衡:较高的值会使输出更忠实于参考图像的结构和样式,但可能会忽略部分文本提示。较低的值(约 1.5)会使文本提示更具影响力。
提示:对于图像编辑任务,我们建议将其设置在 1.2 到 2.0 之间;对于上下文生成任务,更高的 image_guidance_scale 将在输入图像中保留更多细节,我们建议将其设置在 2.5 到 3.0 之间。
max_pixels:当图像的总像素数(宽度×高度)超过此限制时,自动调整图像大小,同时保持其宽高比。这有助于管理性能和内存使用情况。提示:默认值为 1024*1024。如果遇到内存问题,可以减少此值。
max_input_image_side_length:输入图像的最大边长。negative_prompt:告诉模型您不想在图像中看到什么。例如:模糊、低质量、文本、水印
提示:为了获得最佳效果,请尝试不同的否定提示。如果不确定,请使用默认的否定提示。
enable_model_cpu_offload:将 VRAM 使用率降低近 50%,而对速度的影响却微乎其微。这是通过在不使用模型权重时将其卸载到 CPU RAM 来实现的。
enable_sequential_cpu_offload:将 VRAM 使用量最小化至 3GB 以下,但代价是性能会显著降低。其工作原理是将模型卸载到子模块中,然后根据需要按顺序将其加载到 GPU 上。
cfg_range_start,cfg_range_end:定义应用 CFG 的时间步长范围。根据本文,减少cfg_range_end可以显著减少推理时间,而对质量的影响可以忽略不计。scheduler:在 之间选择[euler, dpmsolver++]。默认值为euler。为了以更少的步骤获得更好的性能,请尝试dpmsolver++。num_inference_step:ODE 求解器的离散化步数。默认值为50。enable_teacache:是否启用teacache以加快推理速度。teacache_rel_l1_thresh:时间步嵌入调制噪声输入的累积 L1 距离阈值。它指示是否缓存模型输出。您可以修改此teacache_rel_l1_thresh参数,以在延迟和视觉质量之间取得理想的平衡。默认值 0.05 与基线相比,可提供约30% 的加速。增加此值可以进一步降低延迟,但可能会导致一些细节损失。enable_taylorseer:是否启用taylorseer以加快推理速度。启用后,推理速度可提高2 倍,且与基线相比质量损失可忽略不计。
提高质量的一些建议:
使用高质量图像
提供清晰的图像,最好分辨率大于512×512像素。
输入太小或太模糊将导致输出质量低下。
提供具体的指示
清楚地描述要改变什么以及希望如何改变。
优先考虑英语 该模型目前在英语提示下表现最佳。
更改指令以增强主题一致性。当生成的图像与输入图像不能很好地对齐时,可以尝试以下方法来提高主题一致性:
使用尺寸较大的图像,以及人物占据画面较大比例的图像。
增加图像引导比例,例如增加到 3.0。这样做的代价可能是图像略微过度曝光或显得油腻。
当使用单张输入图像时,可以尝试使用以下提示模板:“她/他......,保持她/他的面部特征,发型和其他属性。”
增加参数——每个提示的图像数量以生成更多输出,让您有更好的机会找到具有更强主题一致性和更令人满意的结果的输出。
较长的提示通常比较短的提示效果更好。对场景和人物互动的更详细描述可以带来额外的好处。
对于上下文编辑(基于多张图片的编辑),我们建议使用以下提示格式:“编辑第一张图片:添加/替换第二张图片中的([物体])[物体]。[目标图片的描述]。” 例如:“编辑第一张图片:添加第二张图片中的男士。该男士正在厨房里与一位女士交谈”。目标图片的描述应尽可能详细。
DEBUG
模型默认是开启flash-attn加速的,所以你可以选择卸载flash-attn,这样模型会回退到pytorch默认加速
因为2080ti显存不到11GB所以需要开启--enable_model_cpu_offload减少显存使用
并且2080ti不支持bfp16,因此要手动修改--dtype fp16
批量生成脚本 .sh
#!/bin/bash
# run_manual_parallel_batch_with_progress.sh
# 切换到脚本所在目录
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
cd $SHELL_FOLDER
# --- 配置参数 ---
MODEL_PATH="OmniGen2/OmniGen2"
OUTPUT_DIR="manual_batch_outputs"
PROMPTS_FILE="prompts.txt" # 提示词输入文件
TEMP_PROMPT_0="${OUTPUT_DIR}/temp_prompts_gpu0.txt"
TEMP_PROMPT_1="${OUTPUT_DIR}/temp_promPTS_gpu1.txt"
DTYPE="fp16"
# 新增:获取格式化时间戳 (年-月-日-时:分)
# %Y-%m-%d-%H:%M:%S -> 2025-10-10-14:22:39
TIMESTAMP=$(date +%Y-%m-%d-%H-%M-%S)
# -----------------
# 确保输出目录存在
mkdir -p $OUTPUT_DIR
# 检查提示词文件是否存在
if [ ! -f "$PROMPTS_FILE" ]; then
echo "错误: 提示词文件 ${PROMPTS_FILE} 不存在!"
echo "请创建该文件,并确保每行包含一个提示词。"
exit 1
fi
echo "--- Starting OmniGen2 Manual Parallel Batch Inference (With Progress) ---"
echo "Model: $MODEL_PATH"
echo "Output Directory: $OUTPUT_DIR"
echo "Prompts File: $PROMPTS_FILE"
echo "Timestamp Prefix: ${TIMESTAMP}" # 打印时间戳前缀
echo "--------------------------------------------------------"
# --- 1. 分割提示词文件 ---
# 获取总行数
TOTAL_LINES=$(grep -cve '^\s*$' "$PROMPTS_FILE") # 统计非空行
# 计算每份的行数(向上取整)
LINES_PER_GPU=$(( (TOTAL_LINES + 1) / 2 ))
echo "总提示词数量: $TOTAL_LINES"
echo "分配给每个 GPU 的数量: $LINES_PER_GPU"
# 使用 head 和 tail 分割文件,平均分配给两个 GPU
grep -ve '^\s*$' "$PROMPTS_FILE" | head -n $LINES_PER_GPU > "$TEMP_PROMPT_0"
grep -ve '^\s*$' "$PROMPTS_FILE" | tail -n +$((LINES_PER_GPU + 1)) > "$TEMP_PROMPT_1"
# 重新计算实际分配的行数
COUNT_GPU0=$(wc -l < "$TEMP_PROMPT_0")
COUNT_GPU1=$(wc -l < "$TEMP_PROMPT_1")
echo "实际分配 GPU 0: $COUNT_GPU0 个提示词"
echo "实际分配 GPU 1: $COUNT_GPU1 个提示词"
# -------------------------
# --- 2. 定义运行子脚本函数 (添加进度逻辑) ---
# 注意:函数现在需要接收和使用 TIMESTAMP 变量
run_inference() {
local gpu_id=$1
local prompt_file=$2
local total_tasks=$3 # 该 GPU 需要完成的任务总数
local timestamp_prefix=$4 # 时间戳前缀
echo "------------------------------------------------"
echo "Starting Process on GPU $gpu_id. Total tasks: $total_tasks"
local index=0
# 逐行读取提示词文件并运行推理
while IFS= read -r instruction; do
# 计算进度
local current_task=$((index + 1))
local percent=0
if [ "$total_tasks" -gt 0 ]; then
percent=$(echo "scale=0; ($current_task * 100) / $total_tasks" | bc)
fi
# --- 核心修改:生成包含时间戳的输出文件名 ---
# 格式: TIMESTAMP-gpu[ID]_[INDEX].png
local output_name="${timestamp_prefix}-gpu${gpu_id}_$(printf "%03d" $index).png"
local output_path="${OUTPUT_DIR}/${output_name}"
# ---------------------------------------------
# 打印进度信息
echo -e "[GPU $gpu_id] Progress: ${percent}% (${current_task}/${total_tasks}) - Saving to: $output_name"
# 执行推理命令
CUDA_VISIBLE_DEVICES=$gpu_id python inference.py \
--model_path $MODEL_PATH \
--num_inference_step 50 \
--height 1024 \
--width 1024 \
--text_guidance_scale 4.0 \
--instruction "$instruction" \
--output_image_path "$output_path" \
--num_images_per_prompt 1 \
--enable_model_cpu_offload \
--dtype $DTYPE
index=$((index + 1))
done < "$prompt_file"
echo "Process on GPU $gpu_id finished. 100% complete."
}
# -----------------------------------------------
# --- 3. 运行并行进程 ---
# 进程 1: 使用 GPU ID 0
( run_inference 0 "$TEMP_PROMPT_0" "$COUNT_GPU0" "$TIMESTAMP" ) &
PID_GPU0=$!
# 进程 2: 使用 GPU ID 1
( run_inference 1 "$TEMP_PROMPT_1" "$COUNT_GPU1" "$TIMESTAMP" ) &
PID_GPU1=$!
# 等待所有后台进程完成
echo "Waiting for both parallel processes (PID $PID_GPU0 and PID $PID_GPU1) to complete..."
wait $PID_GPU0
wait $PID_GPU1
# --- 4. 清理临时文件 ---
rm -f "$TEMP_PROMPT_0" "$TEMP_PROMPT_1"
echo "临时提示词文件已清理。"
# -------------------------
echo "--------------------------------------------------------"
echo "Batch parallel inference finished. All results saved in ${OUTPUT_DIR}"
echo "--------------------------------------------------------"添加可执行权限,执行即可
chmod +x batch_inference.sh
./batch_inference