You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MuseGuard/src/backend/app/scripts/attack_glaze_style_trans.sh

90 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#需要环境conda activate pid
#=============================================================================
# Glaze 风格保护攻击脚本
# 用于保护艺术作品免受 AI 模型的风格模仿
#=============================================================================
### ===================== 环境配置 ===================== ###
export HF_HUB_OFFLINE=1
# 强制使用本地模型缓存,避免联网下载模型
export PYTHONWARNINGS="ignore"
# 忽略所有警告
export CUDA_LAUNCH_BLOCKING=0
# CUDA 异步执行
### ===================== 模型路径 ===================== ###
# Stable Diffusion v1.5 模型路径
export MODEL_PATH="../../static/hf_models/hub/models--runwayml--stable-diffusion-v1-5/snapshots/451f4fe16113bff5a5d2269ed5ad43b0592e9a14"
### ===================== 任务配置 ===================== ###
export TASKNAME="task003"
# 待保护的原始图像目录
export INSTANCE_DIR="../../static/originals/${TASKNAME}"
# 保护后图像的输出目录
export OUTPUT_DIR="../../static/perturbed/${TASKNAME}"
### ===================== 创建必要目录 ===================== ###
echo "=============================================="
echo " 艺术风格保护"
echo "=============================================="
echo ""
echo "创建必要目录..."
mkdir -p "$INSTANCE_DIR"
mkdir -p "$OUTPUT_DIR"
echo "目录创建完成。"
echo ""
### ===================== 清空输出目录 ===================== ###
echo "清空输出目录: $OUTPUT_DIR"
find "$OUTPUT_DIR" -mindepth 1 -delete 2>/dev/null || true
echo "输出目录已清空。"
echo ""
### ===================== 显示配置 ===================== ###
echo "当前配置:"
echo " - 模型路径: $MODEL_PATH"
echo " - 输入目录: $INSTANCE_DIR"
echo " - 输出目录: $OUTPUT_DIR"
echo " - 任务名称: $TASKNAME"
echo ""
# 目标风格 (target_style) 固定可选: Glaze 会将图像的 AI 特征向此风格偏移
# - "impressionism painting by van gogh" (默认,梵高印象派)
# - "abstract art by kandinsky" (康定斯基抽象艺术)
# - "cubism painting by picasso" (毕加索立体主义)
# - "oil painting in baroque style" (巴洛克风格油画)
### ===================== 运行 Glaze ===================== ###
echo "开始风格迁移保护..."
echo ""
CUDA_VISIBLE_DEVICES=0 python ../algorithms/glaze.py \
--pretrained_model_name_or_path=$MODEL_PATH \
--instance_data_dir=$INSTANCE_DIR \
--output_dir=$OUTPUT_DIR \
--resolution=512 \
--center_crop \
--max_train_steps=150 \
--eps=0.04 \
--target_style="impressionism painting by van gogh" \
--style_strength=0.75 \
--n_runs=3 \
--style_transfer_iter=15 \
--guidance_scale=7.5 \
--seed=42
echo ""
echo "=============================================="
echo " 艺术风格防护处理完成!"
echo " 输出保存至: $OUTPUT_DIR"
echo "=============================================="