CryoFM2 是一个基于流模型的冷冻电镜密度图生成式基础模型。 它在经过筛选的 EMDB 半图数据集上进行预训练,以学习高质量冷冻电镜密度图的通用先验知识,并可针对下游任务进行微调。
该模型学习从简单的高斯分布到复杂的冷冻电镜密度图分布的连续映射,实现了稳定的生成能力和灵活的适应能力。CryoFM2 还可作为贝叶斯先验,与任务特定的似然函数自然融合,支持各向异性感知优化、非均匀重建和可控密度修饰等应用。
CryoFM2 在经过筛选的 EMDB 半图数据集上进行预训练,以学习高质量冷冻电镜密度图的通用先验知识。该模型可针对多种下游任务进行微调,如密度图增强和后处理。
预训练架构:
微调架构(用于 EMhancer/EMReady 风格的后处理):
使用 CryoFM2 之前,您需要配置环境并安装软件包。按照以下步骤开始:
# Clone the repository
git clone https://github.com/ByteDance-Seed/cryofm.git
cd cryofm
# Create a new conda environment for CryoFM (recommended)
conda create -n cryofm python=3.10 -y
conda activate cryofm
# Install CryoFM
pip install .从预训练模型生成样本,以探索所学习到的数据分布:
预训练模型:
import torch
from mmengine import Config
from cryofm.core.utils.mrc_io import save_mrc
from cryofm.core.utils.sampling_fm import sample_from_fm
from cryofm.projects.cryofm2.lit_modules import CryoFM2Uncond
# Update the path to your model directory
model_dir = "path/to/cryofm-v2/cryofm2-pretrain"
cfg = Config.fromfile(f"{model_dir}/config.yaml")
lit_model = CryoFM2Uncond.load_from_safetensors(f"{model_dir}/model.safetensors", cfg=cfg)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
lit_model = lit_model.to(device)
lit_model.eval()
def v_xt_t(_xt, _t):
return lit_model(_xt, _t)
# Enable bfloat16 for faster inference if your GPU supports it
with torch.no_grad(), torch.autocast("cuda", dtype=torch.bfloat16):
out = sample_from_fm(
v_xt_t,
lit_model.noise_scheduler,
method="euler",
num_steps=200,
num_samples=3,
device=lit_model.device,
side_shape=64
)
# Apply normalization if configured
if hasattr(lit_model.cfg, "z_scale") and lit_model.cfg.z_scale.mean is not None:
out = out * lit_model.cfg.z_scale.std + lit_model.cfg.z_scale.mean
# Save generated samples
for i in range(3):
save_mrc(out[i].float().cpu().numpy(), f"sample-{i}.mrc", voxel_size=1.5)微调模型(EMhancer/EMReady):
import torch
from mmengine import Config
from cryofm.core.utils.mrc_io import save_mrc
from cryofm.core.utils.sampling_fm import sample_from_fm
from cryofm.projects.cryofm2.lit_modules import CryoFM2Cond
# Choose style: "emhancer" or "emready"
style = "emhancer"
model_dir = f"path/to/cryofm-v2/cryofm2-{style}"
cfg = Config.fromfile(f"{model_dir}/config.yaml")
lit_model = CryoFM2Cond.load_from_safetensors(f"{model_dir}/model.safetensors", cfg=cfg)
output_tag = 1 if style == "emhancer" else 0
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
lit_model = lit_model.to(device)
lit_model.eval()
def v_xt_t(_xt, _t):
bs = _xt.shape[0]
unconditional_generation_conds = {
"input_cond": None,
"output_cond": torch.tensor([output_tag] * bs).to(device),
"vol_cond": None, # dimension should be [bs, d, h, w]
}
return lit_model(_xt, _t, generation_conds=unconditional_generation_conds)
# Enable bfloat16 for faster inference if your GPU supports it
with torch.no_grad(), torch.autocast("cuda", dtype=torch.bfloat16):
out = sample_from_fm(
v_xt_t,
lit_model.noise_scheduler,
method="euler",
num_steps=200,
num_samples=3,
device=lit_model.device,
side_shape=64
)
# Apply normalization if configured
if hasattr(lit_model.cfg, "z_scale") and lit_model.cfg.z_scale.mean is not None:
out = out * lit_model.cfg.z_scale.std + lit_model.cfg.z_scale.mean
# Save generated samples
for i in range(3):
save_mrc(out[i].float().cpu().numpy(), f"{style}-sample-{i}.mrc", voxel_size=1.5)CryoFM2 支持使用预训练模型作为贝叶斯先验进行多种密度图修改操作。支持的操作包括:
基本用法:
python -m cryofm.projects.cryofm2.uncond_sampling \
-i1 half_map_1.mrc \
-i2 half_map_2.mrc \
-o ./output \
--model-dir path/to/cryofm-v2/cryofm2-pretrain \
--op denoise \
--norm-grad \
--use-lamb-w对于修复任务,您需要提供一个 RELION starfile 路径:
python -m cryofm.projects.cryofm2.uncond_sampling \
-i1 half_map_1.mrc \
-i2 half_map_2.mrc \
-o ./output \
--model-dir path/to/cryofm-v2/cryofm2-pretrain \
--op inpaint \
--data-starfile-path path/to/relion_data.star \
--norm-grad \
--use-lamb-wCryoFM2 提供了针对不同风格密度图增强的微调模型,类似于 EMhancer 和 EMReady。
python -m cryofm.projects.cryofm2.cond_sampling \
-i input_map.mrc \
-o ./output_emhancer \
--model-dir path/to/cryofm-v2/cryofm2-emhancer \
--output-tag 1python -m cryofm.projects.cryofm2.cond_sampling \
-i input_map.mrc \
-o ./output_emready \
--model-dir path/to/cryofm-v2/cryofm2-emready \
--output-tag 0 \
--cfg-weight 0.5参数:
-i:输入密度图文件(MRC 格式)-o:输出目录--model-dir:包含 config.yaml 和 model.safetensors 的模型目录路径--output-tag:风格标签(1 表示 EMhancer,0 表示 EMReady)--cfg-weight:无分类器引导权重(可选,默认值因模型而异)accelerate launch 在多 GPU 上实现更快推理:
NCCL_DEBUG=ERROR accelerate launch --num_processes=${NUM_GPUS} --main_process_port=8881 \
python -m cryofm.projects.cryofm2.cond_sampling ...--bf16 标志以减少内存占用并加快推理速度。本模型旨在用于科学研究和结构生物学应用。用户应:
如果您发现 CryoFM2 有用,请引用:
@article{
Li2025.12.29.696802,
author={Li, Yilai and Yuan, Jing and Zhou, Yi and Wang, Zhenghua and Chen, Suyi and Yang, Fengyu and Ling, Haibin and Kovalsky, Shahar Z and Zheng, Xiaoqing and Gu, Quanquan},
title={A Generative Foundation Model for Cryo-EM Densities},
elocation-id={2025.12.29.696802},
year={2025},
doi={10.64898/2025.12.29.696802},
publisher={Cold Spring Harbor Laboratory},
URL={https://www.biorxiv.org/content/early/2025/12/29/2025.12.29.696802},
eprint={https://www.biorxiv.org/content/early/2025/12/29/2025.12.29.696802.full.pdf},
journal={bioRxiv}
}本模型基于 Apache 2.0 许可协议发布。详情请参见 LICENSE 文件。
本研究成果由字节跳动 Seed 团队开发。如需更多信息,请访问: