--- frameworks: - "" language: - en license: apache-2.0 tags: - OneScience - medical multimodal large language model - interactive inference ---

MedGemma

# Model Overview MedGemma is an open-source medical multimodal large language model from Google. It is based on the [Gemma 3](https://ai.google.dev/gemma/docs/core) architecture and is trained for medical text and medical image understanding. MedGemma provides two variants: - **MedGemma 4B**: a multimodal model that supports joint input of medical text and medical images. - **MedGemma 27B**: a text-only model focused on medical text understanding and question answering. # Model Description MedGemma 4B uses an image encoder and has been pretrained on multiple types of de-identified medical data, including chest X-rays (CXR), dermatology images, ophthalmology images, and histopathology slides. Its language model component has been trained on radiology images, pathology images, ophthalmology images, dermatology images, and medical text. Weights and datasets are not available at the moment. They will be uploaded to Hugging Face soon, and command-line downloads will be supported later. # Use Cases | Use case | Description | | :---: | :---: | | Medical question answering | Evaluates the model's question-answering capability on medical knowledge benchmarks such as MedQA. | | Medical image analysis | Supports tasks such as anatomical localization on chest X-rays (CXR) and longitudinal comparison of multi-timepoint images. | | Domain fine-tuning | Performs parameter-efficient fine-tuning with LoRA on datasets such as NCT colon histopathology images. | | Unified inference | Provides interactive and batch-file inference through `MedicalInferenceRunner`. | # Use and Limitations MedGemma is a foundation model for developing healthcare AI applications and is intended to serve as a starting point for downstream development, adaptation, and validation. Developers should fully validate, adapt, and meaningfully modify the model for their specific use case. Outputs generated by MedGemma are not intended to directly inform clinical diagnosis, patient management decisions, treatment recommendations, or any other direct clinical practice. All model outputs should be considered preliminary and require independent verification, clinical correlation, and further investigation. # Usage ## 1. Using OneCode You can try intelligent one-click AI4S programming through the OneCode online environment: [Try intelligent one-click AI4S programming](https://web-2069360198568017922-iaaj.ksai.scnet.cn:58043/home) ## 2. Manual Installation and Usage **Hardware Requirements** - Running on a GPU or DCU is recommended. - CPU can be used for connectivity checks, but it is relatively slow. - DCU users need to install DTK in advance. DTK 25.04.2 or later is recommended, or the OneScience-recommended version that matches the current cluster. **Software Requirements** DCU users who want to learn more about adaptation details can contact liubiao@sugon.com. **Environment Checks** - NVIDIA GPU: ```bash nvidia-smi ``` - Hygon DCU: ```bash hy-smi ``` ### Environment Preparation 1. Check the `botocore` version. If the version is too old, upgrade it: ```bash pip install --upgrade boto3==1.43.36 botocore==1.43.36 ``` 2. Check the `transformers` version. If the version is too old, upgrade it: ```bash pip install --upgrade transformers==5.12.1 ``` ## Quick Start ### 1. Install the Runtime Environment ```bash conda create -n onescience311 python=3.11 -y conda activate onescience311 pip install onescience[bio] -i http://mirrors.onescience.ai:3141/pypi/simple/ --trusted-host mirrors.onescience.ai ``` If the following code cannot find required libraries at runtime, activate CUDA as shown below. ```bash source ${ROCM_PATH}/cuda/env.sh export LD_LIBRARY_PATH="$CONDA_PREFIX/lib:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH="$CONDA_PREFIX/lib/python3.11/site-packages/fastpt/torch/lib:$LD_LIBRARY_PATH" ``` ### 2. Download the Model Package ```bash # By default, the package is downloaded to the model folder under the current path. To change this, adjust the path after local_dir. hf download OneScience-Group/Medgemma --local-dir ./model cd model ``` ### Training Weights and Datasets Weights and datasets are not provided at the moment. They will be uploaded to Hugging Face soon, and command-line downloads will be supported later. ### 3. Usage Modes **Data and Model Weights** By default, the scripts load the model from: ```text ${ONESCIENCE_DATASETS_DIR}/medgemma/modelscope/google/medgemma-1.5-4b-it ``` By default, the scripts load datasets from the following paths: | Task | Data | Default path | |------|------|----------| | MedQA evaluation | MedQA parquet data | `${ONESCIENCE_DATASETS_DIR}/medgemma/medqa` | | Chest X-ray anatomical localization | Chest X-ray images | `${ONESCIENCE_DATASETS_DIR}/medgemma/Chest_Xray/...` | | Chest X-ray longitudinal comparison | Two chest X-rays from before and after | `${ONESCIENCE_DATASETS_DIR}/medgemma/test_compare/...` | | Pathology image fine-tuning | NCT-CRC-HE-100K / CRC-VAL-HE-7K | `${ONESCIENCE_DATASETS_DIR}/medgemma/nct/...` | Download the model in advance and place it in this directory, or override it through the `model_path` environment variable. ### Detailed Usage #### 1. Integration Test Verify whether MedGemma modules, configurations, data adapters, and image processing components can be imported correctly in OneScience: ```bash python tests/test_integration.py ``` --- #### 2. Medical Question Answering Evaluation (`run_evaluate_on_medqa.sh`) Evaluate the model's medical question-answering capability on the MedQA dataset. By default, 10 samples are processed for quick validation. ```bash bash scripts/run_evaluate_on_medqa.sh ``` Output: - `scripts/medqa_results/medqa_results.json`: detailed results for each sample - `scripts/medqa_results/summary.txt`: summary metrics such as accuracy --- #### 3. Chest X-Ray Anatomical Localization (`run_cxr_anatomy.sh`) Perform anatomical localization on one or more chest X-rays. The script runs both single-image mode and batch mode internally: ```bash bash scripts/run_cxr_anatomy.sh ``` Output: - `scripts/outputs/result_*.json`: localization coordinates and labels - `scripts/outputs/result_*.png`: visualization images with bounding-box annotations - `scripts/outputs/batch_summary.json`: summary results for batch mode --- #### 4. Chest X-Ray Longitudinal Comparison (`run_cxr_longitudinal_comparison.sh`) Compare two chest X-rays from the same patient taken at different timepoints: ```bash bash scripts/run_cxr_longitudinal_comparison.sh ``` Output: - `scripts/compare_outputs/compare__vs_.txt`: text comparison report - `scripts/compare_outputs/compare__vs_.json`: structured JSON results --- #### 5. Pathology Image LoRA Fine-Tuning (`run_fine_tune.sh`) Perform LoRA fine-tuning on the NCT colon histopathology image dataset: ```bash bash scripts/run_fine_tune.sh ``` Output: - `scripts/medgemma-nct-lora/`: LoRA weights, training logs, and evaluation results > Note: The script automatically checks and fixes the `boto3==1.43.36` and `botocore==1.43.36` versions to avoid dependency conflicts. --- #### 6. Using the Inference Runner `runner/medical_inference_runner.py` provides a unified inference entry point and supports both interactive and batch-file inference. ##### Interactive Inference ```bash export PYTHONPATH=../../../src:$PYTHONPATH python runner/medical_inference_runner.py \ --config configs/inference_config.yaml \ --interactive ``` ##### Batch-File Inference ```bash export PYTHONPATH=../../../src:$PYTHONPATH python runner/medical_inference_runner.py \ --config configs/inference_config.yaml \ --input data/example_input.json ``` ### Notes - Make sure the `ONESCIENCE_DATASETS_DIR` environment variable is correctly set before running the scripts. - The scripts use `HIP_VISIBLE_DEVICES=0` by default and can run directly on Hygon DCU platforms. On CUDA platforms, replace it with `CUDA_VISIBLE_DEVICES=0` or adjust it according to the available devices. - To use vLLM for accelerated inference, make sure the corresponding version of vLLM is installed and configure `use_vllm: true`. - The chest X-ray anatomical localization and pathology fine-tuning scripts automatically fix `boto3` / `botocore` versions to avoid dependency conflicts. - The 4B multimodal model requires substantial GPU memory for inference. At least 24 GB of memory on a single device is recommended. Multi-device execution can be controlled through `num_gpus` or an external `CUDA_VISIBLE_DEVICES` setting. # Official OneScience Information | Platform | OneScience main repository | Skills repository | | --- | --- | --- | | Gitee | https://gitee.com/onescience-ai/onescience | https://gitee.com/onescience-ai/oneskills | | GitHub | https://github.com/onescience-ai/OneScience | https://github.com/onescience-ai/oneskills | # Citation and License The MedGemma model is licensed under the [Health AI Developer Foundations License](https://developers.google.com/health-ai-developer-foundations/terms), and the example code in this repository is licensed under Apache 2.0. For more information, see: - [Developer documentation](https://developers.google.com/health-ai-developer-foundations/medgemma/get-started) - [Model card](https://developers.google.com/health-ai-developer-foundations/medgemma/model-card) - [Community guidelines](https://developers.google.com/health-ai-developer-foundations/community-guidelines) - [Hugging Face](https://huggingface.co/models?other=medgemma) - [Google Model Garden](https://console.cloud.google.com/vertex-ai/publishers/google/model-garden/medgemma) --- # 中文说明 ## 模型概述 MedGemma 是 Google 开发的开源医疗多模态大语言模型,基于 [Gemma 3](https://ai.google.dev/gemma/docs/core) 架构,训练用于理解医学文本和医学图像。MedGemma 提供两个变体。 - **MedGemma 4B**:支持联合输入医学文本和医学图像的多模态模型。 - **MedGemma 27B**:专注于医学文本理解与问答的纯文本模型。 ## 模型描述 MedGemma 4B 使用图像编码器,并在多类去标识化医疗数据上预训练,包括胸部 X 线片(CXR)、皮肤科图像、眼科图像和组织病理切片。其语言模型组件使用放射影像、病理图像、眼科图像、皮肤科图像和医学文本进行训练。 目前尚未提供权重和数据集。它们将很快上传至 Hugging Face,之后将支持命令行下载。 ## 使用场景 | 使用场景 | 说明 | | :---: | :---: | | 医学问答 | 在 MedQA 等医学知识基准上评估模型的问答能力。 | | 医学图像分析 | 支持胸部 X 线片(CXR)解剖定位及不同时间点图像的纵向比较等任务。 | | 领域微调(fine-tuning) | 在 NCT 结肠组织病理图像等数据集上使用 LoRA 进行参数高效微调。 | | 统一推理(inference) | 通过 `MedicalInferenceRunner` 提供交互式推理和批量文件推理。 | ## 用途与局限 MedGemma 是用于开发医疗 AI 应用的基础模型,旨在作为下游开发、适配和验证的起点。开发者应针对具体使用场景充分验证、适配并实质性修改模型。 MedGemma 生成的输出无意直接用于临床诊断、患者管理决策、治疗建议或其他直接临床实践。所有模型输出均应视为初步结果,需要独立验证、结合临床情况判断并开展进一步调查。 ## 使用方法 ### 1. 使用 OneCode 可通过 OneCode 在线环境体验智能一键 AI4S 编程。 [体验智能一键 AI4S 编程](https://web-2069360198568017922-iaaj.ksai.scnet.cn:58043/home) ### 2. 手动安装与使用 **硬件要求** - 建议在 GPU 或 DCU 上运行。 - 可使用 CPU 检查连通性,但速度较慢。 - DCU 用户需要提前安装 DTK。建议使用 DTK 25.04.2 或更高版本,或使用 OneScience 推荐的与当前集群匹配的版本。 **软件要求** 如需了解更多适配详情,DCU 用户可联系 liubiao@sugon.com。 **环境检查** - NVIDIA GPU ```bash nvidia-smi ``` - Hygon DCU ```bash hy-smi ``` #### 环境准备 1. 检查 `botocore` 版本。如果版本过旧,请升级。 ```bash pip install --upgrade boto3==1.43.36 botocore==1.43.36 ``` 2. 检查 `transformers` 版本。如果版本过旧,请升级。 ```bash pip install --upgrade transformers==5.12.1 ``` ### 快速开始 #### 1. 安装运行环境 ```bash conda create -n onescience311 python=3.11 -y conda activate onescience311 pip install onescience[bio] -i http://mirrors.onescience.ai:3141/pypi/simple/ --trusted-host mirrors.onescience.ai ``` 如果以下代码运行时找不到所需库,请按下文所示激活 CUDA。 ```bash source ${ROCM_PATH}/cuda/env.sh export LD_LIBRARY_PATH="$CONDA_PREFIX/lib:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH="$CONDA_PREFIX/lib/python3.11/site-packages/fastpt/torch/lib:$LD_LIBRARY_PATH" ``` #### 2. 下载模型包 ```bash # By default, the package is downloaded to the model folder under the current path. To change this, adjust the path after local_dir. hf download OneScience-Group/Medgemma --local-dir ./model cd model ``` #### 训练权重与数据集 目前尚未提供权重和数据集。它们将很快上传至 Hugging Face,之后将支持命令行下载。 #### 3. 使用模式 **数据与模型权重** 默认情况下,脚本从以下位置加载模型。 ```text ${ONESCIENCE_DATASETS_DIR}/medgemma/modelscope/google/medgemma-1.5-4b-it ``` 默认情况下,脚本从以下路径加载数据集。 | 任务 | 数据 | 默认路径 | |------|------|----------| | MedQA 评估 | MedQA parquet 数据 | `${ONESCIENCE_DATASETS_DIR}/medgemma/medqa` | | 胸部 X 线片解剖定位 | 胸部 X 线图像 | `${ONESCIENCE_DATASETS_DIR}/medgemma/Chest_Xray/...` | | 胸部 X 线片纵向比较 | 前后两张胸部 X 线片 | `${ONESCIENCE_DATASETS_DIR}/medgemma/test_compare/...` | | 病理图像微调 | NCT-CRC-HE-100K / CRC-VAL-HE-7K | `${ONESCIENCE_DATASETS_DIR}/medgemma/nct/...` | 请提前下载模型并放入此目录,或通过环境变量 `model_path` 覆盖其路径。 #### 详细用法 ##### 1. 集成测试 验证 MedGemma 的模块、配置、数据适配器和图像处理组件能否在 OneScience 中正确导入。 ```bash python tests/test_integration.py ``` --- ##### 2. 医学问答评估(`run_evaluate_on_medqa.sh`) 在 MedQA 数据集上评估模型的医学问答能力。默认处理 10 个样本用于快速验证。 ```bash bash scripts/run_evaluate_on_medqa.sh ``` 输出 - `scripts/medqa_results/medqa_results.json` 包含每个样本的详细结果 - `scripts/medqa_results/summary.txt` 包含准确率等汇总指标 --- ##### 3. 胸部 X 线片解剖定位(`run_cxr_anatomy.sh`) 对一张或多张胸部 X 线片进行解剖定位。脚本内部会运行单图像模式和批处理模式。 ```bash bash scripts/run_cxr_anatomy.sh ``` 输出 - `scripts/outputs/result_*.json` 包含定位坐标及标签 - `scripts/outputs/result_*.png` 包含带边界框标注的可视化图像 - `scripts/outputs/batch_summary.json` 包含批处理模式的汇总结果 --- ##### 4. 胸部 X 线片纵向比较(`run_cxr_longitudinal_comparison.sh`) 对比同一患者在不同时间点拍摄的两张胸部 X 线片。 ```bash bash scripts/run_cxr_longitudinal_comparison.sh ``` 输出 - `scripts/compare_outputs/compare__vs_.txt` 包含文本对比报告 - `scripts/compare_outputs/compare__vs_.json` 包含结构化 JSON 结果 --- ##### 5. 病理图像 LoRA 微调(`run_fine_tune.sh`) 在 NCT 结肠组织病理图像数据集上执行 LoRA 微调。 ```bash bash scripts/run_fine_tune.sh ``` 输出 - `scripts/medgemma-nct-lora/` 包含 LoRA 权重、训练日志及评估结果 > 注意,脚本会自动检查并固定到 `boto3==1.43.36` 和 `botocore==1.43.36` 版本,以避免依赖冲突。 --- ##### 6. 使用推理运行器 `runner/medical_inference_runner.py` 提供统一推理入口,支持交互式推理和批量文件推理。 ###### 交互式推理 ```bash export PYTHONPATH=../../../src:$PYTHONPATH python runner/medical_inference_runner.py \ --config configs/inference_config.yaml \ --interactive ``` ###### 批量文件推理 ```bash export PYTHONPATH=../../../src:$PYTHONPATH python runner/medical_inference_runner.py \ --config configs/inference_config.yaml \ --input data/example_input.json ``` #### 注意事项 - 运行脚本前,请确保正确设置 `ONESCIENCE_DATASETS_DIR` 环境变量。 - 脚本默认使用 `HIP_VISIBLE_DEVICES=0`,可直接在 Hygon DCU 平台上运行。在 CUDA 平台上,将其替换为 `CUDA_VISIBLE_DEVICES=0`,或根据可用设备进行调整。 - 如需使用 vLLM 加速推理,请确保安装对应版本的 vLLM,并配置 `use_vllm: true`。 - 胸部 X 线片解剖定位和病理微调脚本会自动固定 `boto3` / `botocore` 的版本,以避免依赖冲突。 - 4B 多模态模型推理需要大量 GPU 显存,建议单设备显存至少为 24 GB。可通过 `num_gpus` 或外部 `CUDA_VISIBLE_DEVICES` 设置控制多设备运行。 ## OneScience 官方信息 | 平台 | OneScience 主仓库 | Skills 仓库 | | --- | --- | --- | | Gitee | https://gitee.com/onescience-ai/onescience | https://gitee.com/onescience-ai/oneskills | | GitHub | https://github.com/onescience-ai/OneScience | https://github.com/onescience-ai/oneskills | ## 引用与许可证 MedGemma 模型采用 [Health AI Developer Foundations 许可证](https://developers.google.com/health-ai-developer-foundations/terms),本仓库示例代码采用 Apache 2.0 许可证。 更多信息见以下资源。 - [开发者文档](https://developers.google.com/health-ai-developer-foundations/medgemma/get-started) - [模型卡](https://developers.google.com/health-ai-developer-foundations/medgemma/model-card) - [社区准则](https://developers.google.com/health-ai-developer-foundations/community-guidelines) - [Hugging Face](https://huggingface.co/models?other=medgemma) - [Google Model Garden](https://console.cloud.google.com/vertex-ai/publishers/google/model-garden/medgemma)