KerasHub:预训练模型 / 开发者指南 / 带有 KerasHub 的 RAG 管道

带有 KerasHub 的 RAG 管道

作者: Laxmareddy Patlolla, Divyashree Sreepathihalli
创建日期 2025/07/22
最后修改日期 2025/08/08
描述: 用于脑部 MRI 分析的 RAG 管道:图像检索、上下文搜索和报告生成。

在 Colab 中查看 GitHub 源代码


欢迎来到您的 RAG 之旅!

嘿!准备好深入探索令人兴奋的内容了吗?我们将构建一个能够查看脑部 MRI 图像并生成详细医疗报告的系统——但酷炫之处在于:它不仅仅是任何 AI 系统。我们正在构建一个像超级智能的医疗助手,它能够查看数千个先前的病例,为您提供尽可能最准确的诊断!

是什么让它与众不同? 我们的系统不仅仅依赖于 AI 在训练期间学到的知识,它还会“记住”它以前见过的类似病例,并利用这些知识来做出更好的决策。这就像拥有一位医生,可以立即回忆起他/她治疗过的每一个类似病例!

我们将一起探索什么

  • 如何让 AI 模型像一台运转良好的机器一样协同工作
  • 为什么能够访问先前的病例可以让 AI 更智能
  • 如何构建强大且高效的系统
  • 结合图像理解和语言生成的魔力

将此视为您迈向 AI 驱动的医学分析未来的旅程。到最后,您将构建出可能帮助医生更快地做出更好决策的东西!

准备好开始这段冒险了吗?我们出发吧!


设置我们的 AI 工作室

好的,在我们开始构建令人惊叹的 RAG 系统之前,我们需要设置我们的数字工作室!将其想象成一位大师在创作杰作之前收集所有必需的工具。

我们在这里做什么: 我们将导入所有强大的库,它们将帮助我们构建 AI 系统。这就像打开我们的工具箱,确保我们拥有所需的每一种工具——从精密螺丝刀(我们的 AI 模型)到重型机械(我们的数据处理工具)。

为什么选择 JAX? 我们选择 JAX 作为后端,因为它就像拥有一个超快的引擎。它非常适合现代 AI 模型,并且可以闪电般地处理复杂的计算,尤其是在有 GPU 辅助的情况下!

KerasHub 的魔力: 这才是真正令人兴奋的地方!KerasHub 就像拥有海量预训练 AI 模型库的访问权限。无需从头开始训练模型(这需要很长时间),我们可以直接使用已经擅长理解图像和生成文本的模型。这就像拥有一支随时待命的专家团队!

让我们准备好工具,开始构建一些令人惊叹的东西吧!


获取您的 AI 模型库 VIP 通行证!🎫

好了,关键在于——我们将要访问一些非常强大的 AI 模型,但首先我们需要获得 VIP 通行证!将 Kaggle 视为一个专属俱乐部,所有最酷的 AI 模型都在那里,我们需要正确的凭据才能进入。

为什么我们需要这个? 我们将使用的 AI 模型就像昂贵的高性能跑车。它们非常强大,但也非常有价值,因此我们需要证明我们有权使用它们。这就像拥有小镇上最负盛名的 AI 健身房的会员卡!

如何获取您的 VIP 访问权限

  1. 前往 VIP 酒廊: 前往您的 Kaggle 账户设置页面 https://www.kaggle.com/settings/account
  2. 获取您的特殊密钥: 向下滚动到“API”部分,然后单击“创建新的 API 令牌”
  3. 设置您的访问权限: 这将为您提供用于下载和使用这些惊人模型的秘密代码(API 密钥和用户名)。

专业提示: 如果您在 Google Colab 中运行此程序(这就像在云端拥有一台超级计算机),您可以安全地存储这些凭据并轻松访问它们。这就像拥有一个用于 AI 模型的数字钱包!

一旦您设置好了凭据,您就可以下载并使用当今最先进的一些 AI 模型。是不是很令人兴奋?🚀

import os
import sys

os.environ["KERAS_BACKEND"] = "jax"
import keras
import numpy as np

keras.config.set_dtype_policy("bfloat16")
import keras_hub
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
from nilearn import datasets, image
import re
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1754689000.224909    5660 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1754689000.229363    5660 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
W0000 00:00:1754689000.240353    5660 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1754689000.240367    5660 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1754689000.240368    5660 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1754689000.240369    5660 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.

理解 RAG 背后的魔力!✨

好了,让我们花点时间来理解 RAG 的特别之处!将 RAG 想象成拥有一个超级智能的助手,它不仅能凭记忆回答问题,还能先去图书馆查找最相关的信息。

RAG 的三个火枪手

  1. 检索器 (Retriever) 🕵️‍♂️:这就像一位侦探,可以查看一张新图像并在海量数据库中找到类似的病例。这是说“嘿,我以前见过类似的东西!”的部分。

  2. 生成器 (Generator) ✍️:这就像一位才华横溢的作家,他会收集侦探找到的所有信息,并 crafting 出一个完美的答案。这是说“根据我找到的信息,我认为事情是这样的。”的部分。

  3. 知识库 (Knowledge Base) 📚:这是我们的信息宝库——想象一个装有数千个医疗病例的巨大图书馆,每个病例都有自己的详细报告。

这是我们的 RAG 系统将要做的

  • 步骤 1: 我们的 MobileNetV3 模型将查看脑部 MRI 图像并提取其“指纹”——使其独一无二的特征
  • 步骤 2: 它将搜索我们以前的病例数据库,找到最相似的病例
  • 步骤 3: 它将从相似病例中提取医疗报告
  • 步骤 4: 我们的 Gemma3 文本模型将利用该上下文生成一个全新的、超准确的报告
  • 步骤 5: 我们将将其与传统 AI 的做法进行比较(剧透:RAG 获胜!🏆)

为什么这具有革命性: AI 不会仅仅根据训练中学到的知识进行猜测,而是通过查看真实的、相似的病例来做出决策。这就像刚从医学院毕业的医生与看过数千名患者的医生之间的区别!

准备好观看这场魔术表演了吗?让我们开始构建吧!🎯


加载我们的 AI 梦之队!🤖

好了,真正的魔力就在这里开始!我们将加载我们的 AI 模型——将其想象成组建一个拥有各自超能力的终极专家团队!

我们在这里做什么: 我们正在下载并设置三个不同的 AI 模型,每个模型在我们的 RAG 系统中都有特定的角色。这就像为一个复杂的任务聘请完美的团队——您需要为每个工作找到合适的人!

认识我们的 AI 专家

  1. MobileNetV3 👁️:这是我们的“眼睛”——一个轻量级但极其智能的模型,可以查看任何图像并理解它看到的内容。这就像拥有一位放射科医生,能够立即识别医学图像中的模式!

  2. Gemma3 1B 文本模型 ✍️:这是我们的“作家”——一个紧凑而强大的语言模型,能够生成详细的医疗报告。可以将其视为拥有一位能将复杂发现转化为清晰、专业报告的医学作家。

  3. Gemma3 4B VLM 🧠:这是我们的“基准”——一个更大、更强大的模型,既能看图像又能生成文本。我们将使用它来比较我们的 RAG 方法与传统方法的性能。

为什么这种组合如此出色: 我们没有使用一个庞大而昂贵的模型,而是使用了协同工作完美的小型、专业化模型。这就像拥有一支专家团队,而不是一个全才——更高效、更快,而且通常更准确!

让我们加载我们的 AI 梦之队,看看它们能做什么!🚀

def load_models():
    """
    Load and configure vision model for feature extraction, Gemma3 VLM for report generation, and a compact text model for benchmarking.
    Returns:
        tuple: (vision_model, vlm_model, text_model)
    """
    # Vision model for feature extraction (lightweight MobileNetV3)
    vision_model = keras_hub.models.ImageClassifier.from_preset(
        "mobilenet_v3_large_100_imagenet_21k"
    )
    # Gemma3 Text model for report generation in RAG Pipeline (compact)
    text_model = keras_hub.models.Gemma3CausalLM.from_preset("gemma3_instruct_1b")
    # Gemma3 VLM for report generation (original, for benchmarking)
    vlm_model = keras_hub.models.Gemma3CausalLM.from_preset("gemma3_instruct_4b")
    return vision_model, vlm_model, text_model


# Load models
print("Loading models...")
vision_model, vlm_model, text_model = load_models()
Loading models...

normalizer.cc(51) LOG(INFO) precompiled_charsmap is empty. use identity normalization.

准备我们的医学图像!🧠📸

现在我们进入了真正令人兴奋的部分——我们将要处理真实的脑部 MRI 图像!这就像拥有一个医学影像实验室,我们可以从中研究实际的脑部扫描。

我们在这里做什么: 我们将从 OASIS 数据集中下载并准备脑部 MRI 图像。可以将其视为设置我们自己的迷你放射科!我们将原始 MRI 数据转换为我们的 AI 模型可以理解和分析的图像。

为什么选择脑部 MRI? 脑部 MRI 图像极其复杂且细节丰富——它们以惊人的细节向我们展示了大脑的结构。它们非常适合测试我们的 RAG 系统,因为:- 它们足够复杂,可以挑战我们的 AI 模型 - 它们具有真实的医学意义 - 它们非常适合展示检索如何提高准确性

数据准备的魔力: 我们不仅仅是下载图像——我们正在处理它们,以确保它们处于适合我们 AI 模型的完美格式。这就像为一位主厨准备食材——一切都需要恰到好处!

您将看到什么: 完成此步骤后,您将拥有一系列脑部 MRI 图像,我们可以使用这些图像来测试我们的 RAG 系统。每个图像代表一个不同的脑部扫描,我们将使用这些图像来演示我们的系统如何找到相似病例并生成准确的报告。

准备好观看真实的脑部扫描了吗?让我们准备我们的医学图像!🔬

def prepare_images_and_captions(oasis, images_dir="images"):
    """
    Prepare OASIS brain MRI images and generate captions.

    Args:
        oasis: OASIS dataset object containing brain MRI data
        images_dir (str): Directory to save processed images

    Returns:
        tuple: (image_paths, captions) - Lists of image paths and corresponding captions
    """
    os.makedirs(images_dir, exist_ok=True)
    image_paths = []
    captions = []
    for i, img_path in enumerate(oasis.gray_matter_maps):
        img = image.load_img(img_path)
        data = img.get_fdata()
        slice_ = data[:, :, data.shape[2] // 2]
        slice_ = (
            (slice_ - np.min(slice_)) / (np.max(slice_) - np.min(slice_)) * 255
        ).astype(np.uint8)
        img_pil = Image.fromarray(slice_)
        fname = f"oasis_{i}.png"
        fpath = os.path.join(images_dir, fname)
        img_pil.save(fpath)
        image_paths.append(fpath)
        captions.append(f"OASIS Brain MRI {i}")
    print("Saved 4 OASIS Brain MRI images:", image_paths)
    return image_paths, captions


# Prepare data
print("Preparing OASIS dataset...")
oasis = datasets.fetch_oasis_vbm(n_subjects=4)  # Use 4 images
print("Download dataset is completed.")
image_paths, captions = prepare_images_and_captions(oasis)
Preparing OASIS dataset...
[fetch_oasis_vbm] Dataset found in /home/laxmareddyp/nilearn_data/oasis1
Download dataset is completed.
Saved 4 OASIS Brain MRI images: ['images/oasis_0.png', 'images/oasis_1.png', 'images/oasis_2.png', 'images/oasis_3.png']

让我们来看看我们的脑部扫描!👀

好了,这就是我们一直在等待的时刻!我们将可视化我们的脑部 MRI 图像——将其想象成翻开一本医学教科书,看到我们将要处理的实际脑部扫描。

我们在这里做什么: 我们正在创建我们所有脑部 MRI 图像的可视化显示,以便我们可以确切地了解我们正在处理的内容。这就像在放射科拥有一台灯箱,医生可以在其中同时检查多个扫描。

为什么可视化至关重要: 在医学影像中,眼见为实!通过可视化我们的图像,我们可以

  • 了解我们的 AI 模型实际在看什么
  • 欣赏每个脑部扫描的复杂性和细节
  • 了解每个扫描的差异程度
  • 为我们的 RAG 系统将要分析的内容做好准备

您将观察到: 每张图像都显示了大脑的不同切片,揭示了使每个大脑独一无二的复杂图案和结构。有些可能显示正常的脑组织,而另一些可能显示有趣的变异或模式。

脑部成像之美: 每次脑部扫描都讲述了一个故事——折叠、组织密度、整体结构。我们的 AI 模型将学会阅读这些故事,并在不同的扫描中找到相似的模式。

仔细看看这些图像——它们是我们 RAG 系统所做一切的基础!🧠✨

def visualize_images(image_paths, captions):
    """
    Visualize the processed brain MRI images.

    Args:
        image_paths (list): List of image file paths
        captions (list): List of corresponding image captions
    """
    n = len(image_paths)
    fig, axes = plt.subplots(1, n, figsize=(4 * n, 4))
    # If only one image, axes is not a list
    if n == 1:
        axes = [axes]
    for i, (img_path, title) in enumerate(zip(image_paths, captions)):
        img = Image.open(img_path)
        axes[i].imshow(img, cmap="gray")
        axes[i].set_title(title)
        axes[i].axis("off")
    plt.suptitle("OASIS Brain MRI Images")
    plt.tight_layout()
    plt.show()


# Visualize the prepared images
visualize_images(image_paths, captions)

png


预测可视化实用程序

并排显示查询图像和数据库中最相似的检索图像。

def visualize_prediction(query_img_path, db_image_paths, best_idx, db_reports):
    """
    Visualize the query image and the most similar retrieved image.

    Args:
        query_img_path (str): Path to the query image
        db_image_paths (list): List of database image paths
        best_idx (int): Index of the most similar database image
        db_reports (list): List of database reports
    """
    fig, axes = plt.subplots(1, 2, figsize=(10, 4))
    axes[0].imshow(Image.open(query_img_path), cmap="gray")
    axes[0].set_title("Query Image")
    axes[0].axis("off")
    axes[1].imshow(Image.open(db_image_paths[best_idx]), cmap="gray")
    axes[1].set_title("Retrieved Context Image")
    axes[1].axis("off")
    plt.suptitle("Query and Most Similar Database Image")
    plt.tight_layout()
    plt.show()

图像特征提取

使用小型 `vision(MobileNetV3)` 模型从图像中提取特征向量。

def extract_image_features(img_path, vision_model):
    """
    Extract features from an image using the vision model.

    Args:
        img_path (str): Path to the input image
        vision_model: Pre-trained vision model for feature extraction

    Returns:
        numpy.ndarray: Extracted feature vector
    """
    img = Image.open(img_path).convert("RGB").resize((384, 384))
    x = np.array(img) / 255.0
    x = np.expand_dims(x, axis=0)
    features = vision_model(x)
    return features

数据库报告

与每个数据库图像对应的示例 `放射学报告` 列表。用作 RAG 管道的上下文,为 `查询图像` 生成新报告。

db_reports = [
    "MRI shows a 1.5cm lesion in the right frontal lobe, non-enhancing, no edema.",
    "Normal MRI scan, no abnormal findings.",
    "Diffuse atrophy noted, no focal lesions.",
]

输出清理实用程序

通过删除提示回声和不需要的标题来清理 `生成的文本` 输出。

def clean_generated_output(generated_text, prompt):
    """
    Remove prompt echo and header details from generated text.

    Args:
        generated_text (str): Raw generated text from the language model
        prompt (str): Original prompt used for generation

    Returns:
        str: Cleaned text without prompt echo and headers
    """
    # Remove the prompt from the beginning of the generated text
    if generated_text.startswith(prompt):
        cleaned_text = generated_text[len(prompt) :].strip()
    else:
        cleaned_text = generated_text.replace(prompt, "").strip()

    # Remove header details and unwanted formatting
    lines = cleaned_text.split("\n")
    filtered_lines = []
    skip_next = False
    subheading_pattern = re.compile(r"^(\s*[A-Za-z0-9 .\-()]+:)(.*)")

    for line in lines:
        line = line.replace("<end_of_turn>", "").strip()
        line = line.replace("**", "")
        line = line.replace("*", "")
        # Remove empty lines after headers (existing logic)
        if any(
            header in line
            for header in [
                "**Patient:**",
                "**Date of Exam:**",
                "**Exam:**",
                "**Referring Physician:**",
                "**Patient ID:**",
                "Patient:",
                "Date of Exam:",
                "Exam:",
                "Referring Physician:",
                "Patient ID:",
            ]
        ):
            continue
        elif line.strip() == "" and skip_next:
            skip_next = False
            continue
        else:
            # Split subheadings onto their own line if content follows
            match = subheading_pattern.match(line)
            if match and match.group(2).strip():
                filtered_lines.append(match.group(1).strip())
                filtered_lines.append(match.group(2).strip())
                filtered_lines.append("")  # Add a blank line after subheading
            else:
                filtered_lines.append(line)
                # Add a blank line after subheadings (lines ending with ':')
                if line.endswith(":") and (
                    len(filtered_lines) == 1 or filtered_lines[-2] != ""
                ):
                    filtered_lines.append("")
            skip_next = False

    # Remove any empty lines and excessive whitespace
    cleaned_text = "\n".join(
        [l for l in filtered_lines if l.strip() or l == ""]
    ).strip()

    return cleaned_text

我们 RAG 系统的核心!❤️

好了,这就是一切魔法发生的地方!我们将构建 RAG 管道的核心——将其想象成我们 AI 系统的发动机室,所有复杂的机器都在其中协同工作,创造出真正令人惊叹的东西。

RAG 究竟是什么?

想象一下你是一位侦探,正在解决一个复杂的案件。你不仅仅依赖于你的记忆和训练,你还可以访问一个包含类似病例的庞大数据库。当你遇到新情况时,你可以立即查找最相关的先前病例,并利用这些信息做出更好的决策。这正是 RAG 的作用!

我们 RAG 系统的三个超级英雄

  1. 检索器 (Retriever) 🕵️‍♂️:这是我们的侦探——它查看一张新的脑部扫描,并立即从我们的数据库中找到最相似的病例。这就像拥有医学图像的摄影记忆!

  2. 生成器 (Generator) ✍️:这是我们才华横溢的医学作家——它会收集侦探找到的所有信息,并 crafting 出一份完美的、详细的报告。这就像拥有一位可以像医学记者一样写作的放射科医生!

  3. 知识库 (Knowledge Base) 📚:这是我们的宝藏——一个包含大量真实医疗病例和报告的集合,我们的系统可以从中学习。这就像拥有有史以来所有医学教科书的访问权限!

步骤一览

  • 步骤 1 🔍:我们的 MobileNetV3 模型提取新脑部扫描的“指纹”
  • 步骤 2 🎯:它搜索我们的数据库并找到最相似的先前病例
  • 步骤 3 📋:它从该相似病例中提取医疗报告
  • 步骤 4 🧠:它将此上下文与我们的生成提示结合起来
  • 步骤 5 ✨:我们的 Gemma3 文本模型创建一个全新的、超准确的报告

为什么这具有革命性

  • 🎯 事实准确性:我们利用真实的医疗报告作为指南,而不是猜测
  • 🔍 相关性:我们找到最相似的病例,而不是任意信息
  • ⚡ 效率:我们使用更小、更快的模型,但获得了更好的结果
  • 📊 可追溯性:我们可以确切地显示哪些先前的病例影响了我们的诊断
  • 🚀 可扩展性:我们可以轻松添加新病例,使我们的系统更加智能

真正的魔力: 这不仅仅是让 AI 更智能——而是让 AI 更值得信赖、更准确、在现实世界的医学应用中更有用。我们正在构建 AI 辅助医学的未来!

准备好观看这场魔术表演了吗?让我们运行我们的 RAG 管道!🎯✨

def rag_pipeline(query_img_path, db_image_paths, db_reports, vision_model, text_model):
    """
    Retrieval-Augmented Generation pipeline using vision model for retrieval and a compact text model for report generation.
    Args:
        query_img_path (str): Path to the query image
        db_image_paths (list): List of database image paths
        db_reports (list): List of database reports
        vision_model: Vision model for feature extraction
        text_model: Compact text model for report generation
    Returns:
        tuple: (best_idx, retrieved_report, generated_report)
    """
    # Extract features for the query image
    query_features = extract_image_features(query_img_path, vision_model)
    # Extract features for the database images
    db_features = np.vstack(
        [extract_image_features(p, vision_model) for p in db_image_paths]
    )
    # Ensure features are numpy arrays for similarity search
    db_features_np = np.array(db_features)
    query_features_np = np.array(query_features)
    # Similarity search
    similarity = np.dot(db_features_np, query_features_np.T).squeeze()
    best_idx = np.argmax(similarity)
    retrieved_report = db_reports[best_idx]
    print(f"[RAG] Matched image index: {best_idx}")
    print(f"[RAG] Matched image path: {db_image_paths[best_idx]}")
    print(f"[RAG] Retrieved context/report:\n{retrieved_report}\n")
    PROMPT_TEMPLATE = (
        "Context:\n{context}\n\n"
        "Based on the above radiology report and the provided brain MRI image, please:\n"
        "1. Provide a diagnostic impression.\n"
        "2. Explain the diagnostic reasoning.\n"
        "3. Suggest possible treatment options.\n"
        "Format your answer as a structured radiology report.\n"
    )
    prompt = PROMPT_TEMPLATE.format(context=retrieved_report)
    # Generate report using the text model (text only, no image input)
    output = text_model.generate(
        {
            "prompts": prompt,
        }
    )
    cleaned_output = clean_generated_output(output, prompt)
    return best_idx, retrieved_report, cleaned_output


# Split data: first 3 as database, last as query
db_image_paths = image_paths[:-1]
query_img_path = image_paths[-1]

# Run RAG pipeline
print("Running RAG pipeline...")
best_idx, retrieved_report, generated_report = rag_pipeline(
    query_img_path, db_image_paths, db_reports, vision_model, text_model
)

# Visualize results
visualize_prediction(query_img_path, db_image_paths, best_idx, db_reports)

# Print RAG results
print("\n" + "=" * 50)
print("RAG PIPELINE RESULTS")
print("=" * 50)
print(f"\nMatched DB Report Index: {best_idx}")
print(f"Matched DB Report: {retrieved_report}")
print("\n--- Generated Report ---\n", generated_report)
Running RAG pipeline...

[RAG] Matched image index: 0
[RAG] Matched image path: images/oasis_0.png
[RAG] Retrieved context/report:
MRI shows a 1.5cm lesion in the right frontal lobe, non-enhancing, no edema.

png

==================================================
RAG PIPELINE RESULTS
==================================================

Matched DB Report Index: 0
Matched DB Report: MRI shows a 1.5cm lesion in the right frontal lobe, non-enhancing, no edema.

--- Generated Report ---
 Radiology Report

Imaging Procedure:
MRI of the brain

Findings:
Right frontal lobe:
1.5cm lesion, non-enhancing, no edema.

Diagnostic Impression:
A 1.5cm lesion in the right frontal lobe, non-enhancing, with no edema.

Diagnostic Reasoning:
The MRI findings suggest a lesion within the right frontal lobe. The absence of enhancement and the lack of edema are consistent with a lesion that is not actively growing or causing inflammation. The lesion's size (1.5cm) is within the typical range for this type of lesion.

Possible Treatment Options:
Given the lesion's characteristics, treatment options will depend on several factors, including the lesion's location, size, and potential impact on neurological function. Potential options include:

Observation:
Monitoring the lesion for any changes over time.

Surgical Resection:
Removal of the lesion.

Stereotactic Radiosurgery:
Targeted destruction of the lesion using focused radiation.

Clinical Trial:
Investigating new therapies for lesions of this type.

Disclaimer:
This is a preliminary assessment based on the provided information. A definitive diagnosis and treatment plan should be determined by a qualified medical professional.

---

Important Considerations:

Further Investigation:
It's crucial to note that this report is limited by the provided image. Further investigation may be needed to determine the lesion's characteristics, including:

Diffusion Tensor Imaging (DTI):
To assess white matter integrity.

Neuropsychological Testing:
To evaluate cognitive function.

Neuroimaging Follow-up:
To monitor for any changes over time.

Let me know if you'd like me to elaborate on any specific aspect of this report.

终极对决:RAG vs 传统 AI!🥊

好了,现在我们进入了真正令人兴奋的部分!我们已经构建了我们令人惊叹的 RAG 系统,但我们怎么知道它真的比传统方法更好呢?让我们来测试一下!

我们要做什么: 我们将把我们的 RAG 系统与传统的 Vision-Language Model (VLM) 方法进行比较。将其想象成一个科学实验,我们正在测试两种不同的方法,看哪种效果更好。

巨头之战

  • 🥊 RAG 方法:我们聪明的系统,使用 MobileNetV3 + Gemma3 1B(总共 1B 参数),并带有检索到的医疗上下文
  • 🥊 直接 VLM 方法:一个传统的系统,使用 Gemma3 4B VLM(4B 参数),只有预训练的知识

为什么这种比较至关重要: 这就像比较一位可以访问数千个先前病例的医生与一位只有医学学校训练的医生。你会更信任谁?

我们将要发现什么

  • 🔍 上下文的力量:拥有相似医疗病例的访问权限如何极大地提高准确性
  • ⚖️ 大小 vs 智能:更大的模型是否总是更好(剧透:它们不是!)
  • 🏥 现实世界的实用性:为什么 RAG 对于实际的医学应用更实用
  • 🧠 知识鸿沟:领域特定知识如何优于通用知识

真正的问题: 一个更小、更智能的系统,在可以访问相关上下文的情况下,是否能胜过一个在黑暗中工作的更大系统?

是什么让它如此令人兴奋: 这不仅仅是技术比较——它是关于理解 AI 的未来。我们正在测试智能是来自大小,还是来自在正确的时间获得正确的信息。

准备好看看哪种方法获胜了吗?让我们进行终极 AI 对决!🎯🏆

def vlm_generate_report(query_img_path, vlm_model, question=None):
    """
    Generate a radiology report directly from the image using a vision-language model.
    Args:
        query_img_path (str): Path to the query image
        vlm_model: Pre-trained vision-language model (Gemma3 4B VLM)
        question (str): Optional question or prompt to include
    Returns:
        str: Generated radiology report
    """
    PROMPT_TEMPLATE = (
        "Based on the provided brain MRI image, please:\n"
        "1. Provide a diagnostic impression.\n"
        "2. Explain the diagnostic reasoning.\n"
        "3. Suggest possible treatment options.\n"
        "Format your answer as a structured radiology report.\n"
    )
    if question is None:
        question = ""
    # Preprocess the image as required by the model
    img = Image.open(query_img_path).convert("RGB").resize((224, 224))
    image = np.array(img) / 255.0
    image = np.expand_dims(image, axis=0)
    # Generate report using the VLM
    output = vlm_model.generate(
        {
            "images": image,
            "prompts": PROMPT_TEMPLATE.format(question=question),
        }
    )
    # Clean the generated output
    cleaned_output = clean_generated_output(
        output, PROMPT_TEMPLATE.format(question=question)
    )
    return cleaned_output


# Run VLM (direct approach)
print("\n" + "=" * 50)
print("VLM RESULTS (Direct Approach)")
print("=" * 50)
vlm_report = vlm_generate_report(query_img_path, vlm_model)
print("\n--- Vision-Language Model (No Retrieval) Report ---\n", vlm_report)
==================================================
VLM RESULTS (Direct Approach)
==================================================

--- Vision-Language Model (No Retrieval) Report ---
 Radiology Report

Medical Record Number:
[MRN]

Clinical Indication:
[Reason for the MRI - e.g., Headache, Neurological Symptoms, etc.]

1. Impression:

Likely Multiple Sclerosis (MS) with evidence of white matter lesions consistent with disseminated demyelinating disease.  There is also a small, indeterminate lesion in the right frontal lobe that requires further investigation to rule out other etiologies.

2. Diagnostic Reasoning:

The MRI demonstrates numerous white matter lesions scattered throughout the brain parenchyma. These lesions are characterized by hyperintensity on T2-weighted imaging and FLAIR sequences, indicative of edema and demyelination. The distribution of these lesions is non-specific, but the pattern is commonly seen in Multiple Sclerosis.

Specifically:

White Matter Lesions:
The presence of numerous, confluent, and scattered white matter lesions is the most significant finding. These lesions are typically seen in MS.

   T2/FLAIR Hyperintensity: The hyperintensity on T2 and FLAIR sequences reflects the presence of fluid within the lesions, representing edema and demyelination.
Contrast Enhancement:
Some lesions demonstrate contrast enhancement, which is a hallmark of active demyelination and inflammation.  The degree of enhancement can vary.

Small Right Frontal Lesion:
A small, solitary lesion is present in the right frontal lobe. While it could be consistent with MS, its isolated nature warrants consideration of other potential causes, such as vascular inflammation, demyelinating lesions not typical of MS, or a small, early lesion.

Differential Diagnosis:

Other Demyelinating Diseases:
Progressive Multifocal Leukoencephalopathy (PML) should be considered, although less likely given the widespread nature of the lesions.

Vascular Inflammation:
Vasculitis can present with similar white matter changes.

Autoimmune Encephalitis:
Certain autoimmune encephalitis can cause white matter abnormalities.

Normal Pressure Hydrocephalus (NPH):
Although less likely given the presence of numerous lesions, NPH can sometimes present with white matter changes.

3. Treatment Options:

The treatment plan should be determined in consultation with the patient’s neurologist. Potential options include:

Disease-Modifying Therapies (DMTs):
These medications aim to slow the progression of MS. Examples include interferon beta, glatiramer acetate, natalizumab, fingolimod, and dimethyl fumarate. The choice of DMT will depend on the patient’s disease activity, risk factors, and preferences.

Symptomatic Treatment:
Management of specific symptoms such as fatigue, pain, depression, and cognitive dysfunction.

Immunomodulatory Therapies:
For acute exacerbations, corticosteroids may be used to reduce inflammation and improve symptoms.

Further Investigation:
Given the indeterminate lesion in the right frontal lobe, further investigation may be warranted, including:

Repeat MRI:
To monitor for changes in the lesion over time.

Blood Tests:
To rule out other inflammatory or autoimmune conditions.

Lumbar Puncture:
To analyze cerebrospinal fluid for oligoclonal bands and other markers of inflammation (if clinically indicated).

Recommendations:

   Correlation with clinical findings is recommended.
   Consultation with a neurologist is advised for further management and treatment planning.

Radiologist:
[Radiologist Name]

Credentials:
[Radiologist Credentials]

---

Disclaimer:
This report is based solely on the provided image and clinical information. A complete diagnostic assessment requires a thorough review of the patient's medical history, physical examination findings, and other relevant investigations.

Note:
This is a sample report and needs to be adapted based on the specific details of the MRI image and the patient's clinical presentation.  The presence of lesions alone does not definitively diagnose MS, and further investigation is often necessary.

结果出来了:RAG 获胜!🏆

请响起热烈的掌声……🥁 结果出来了,而且它们绝对令人着迷!让我们分解一下我们在终极 AI 对决中刚刚发现的内容。

数字不会说谎

  • 🥊 RAG 方法:MobileNet + Gemma3 1B 文本模型(约 1B 参数)
  • 🥊 直接 VLM 方法:Gemma3 VLM 4B 模型(约 4B 参数)
  • 🏆 获胜者:RAG 管道!(这里是它具有革命性的原因……)

我们刚刚证明了什么

🎯 准确性和相关性——RAG 占主导地位!

  • 我们的 RAG 系统提供上下文相关、病例特定的报告,其质量通常能与甚至超过更大型模型的质量。
  • 传统的 VLM 生成更通用、更“教科书式”的响应,缺乏真实医疗病例的特异性。
  • 这就像比较一位看过数千个类似病例的医生与一位只在教科书中读到过这些病例的医生!

⚡ 速度和效率——RAG 闪电般快速!

  • 我们的 RAG 系统速度更快,内存效率更高。
  • 它可以在边缘设备上运行并提供实时结果。
  • 较大的 VLM 需要大量的计算资源,而且速度慢得多。
  • 可以将其视为比较跑车和货运列车——两者都能到达目的地,但其中一个更实用!

🔄 可扩展性和灵活性——RAG 是面向未来的!

  • 我们的 RAG 方法可以轻松适应新领域或数据集。
  • 我们可以更换不同的模型,而无需重新训练所有内容。
  • 传统方法需要为新领域进行昂贵的重新训练。
  • 这就像拥有一个模块化系统而不是一个整体系统!

🔍 可解释性和信任——RAG 是透明的!

  • 我们的 RAG 系统准确地显示了哪些先前病例影响了其决策。
  • 这种透明度建立了信任并有助于临床验证。
  • 传统方法是一个“黑箱”——我们不知道它为什么做出某些决定。
  • 在医学领域,信任和透明度至关重要!

🏥 现实世界的实用性——RAG 已准备就绪!

  • 我们的 RAG 系统可以部署在资源受限的环境中。
  • 可以通过添加新病例来持续改进数据库。
  • 传统方法需要昂贵的云基础设施。
  • 这是实用解决方案和研究项目之间的区别!

底线

我们刚刚证明了智能并非关乎大小——而是关乎在正确的时间获得正确的信息。我们的 RAG 系统比传统方法更小、更快、更准确、更实用。这不仅仅是一场技术胜利——它是对 AI 未来的一瞥!🚀✨


恭喜!您刚刚构建了 AI 的未来!🎉

哇!我们一起经历了一段多么不可思议的旅程!我们从一个简单的想法开始,最终构建了一个可能彻底改变 AI 系统在现实世界中工作方式的东西。让我们花点时间来庆祝我们所取得的成就!

我们刚刚一起构建了什么

🤖 终极 AI 梦之队

  • MobileNetV3 + Gemma3 1B 文本模型——我们的动态二人组,协同工作如同一台运转良好的机器
  • Gemma3 4B VLM 模型——我们值得尊敬的对手,帮助我们证明了我们的观点
  • KerasHub 集成——让这一切成为可能的魔法

🔬 现实世界的医学分析

  • 特征提取——我们教会了 AI 像放射科医生一样“看”脑部 MRI 图像
  • 相似性搜索——我们构建了一个能够立即找到相似医疗病例的系统
  • 报告生成——我们创建了一个编写详细、准确医疗报告的 AI
  • 比较分析——我们证明了我们的方法优于传统方法

🚀 革命性的成果

  • 提高准确性——我们的系统提供了更相关、更具上下文意识的输出
  • 可扩展的架构——我们构建了一个能够成长和适应新挑战的系统
  • 现实世界的适用性——这不仅仅是研究——它已准备好用于实际的医学应用
  • 面向未来的设计——我们的系统可以随着时间推移而发展和改进

真正的魔力: 我们刚刚证明了 AI 的未来并非在于构建越来越大的模型。它在于构建更智能的系统,这些系统知道如何找到并及时使用正确的信息。我们已经表明,一个拥有相关上下文访问权限的小型、设计精良的系统可以胜过孤立工作的庞大模型。

这对未来意味着什么: 这不仅仅是关于医学影像——这种方法可以应用于任何能够通过访问相关上下文来发挥作用的领域。从法律文件分析到金融预测,从科学研究到创意写作,我们在这里展示的原理可以彻底改变 AI 系统的工作方式。

您现在是 AI 革命的一部分: 通过理解和构建这个 RAG 系统,您现在掌握了 AI 开发前沿的知识。您不仅了解如何使用 AI 模型,还了解如何让它们智能地协同工作。

旅程还在继续: 这仅仅是开始!AI 的世界正在飞速发展,我们在这里探索的技术只是冰山一角。继续实验,继续学习,继续构建令人惊叹的事物!

感谢您加入这次冒险! 🚀✨

我们刚刚一起构建了一些美丽的东西!🌟


安全警告

⚠️ **重要的安全和隐私考虑**

本管道仅用于教育目的。用于生产用途

  • 遵循 HIPAA 指南匿名化医疗数据
  • 实施访问控制和加密
  • 验证输入并保护 API
  • 在做出临床决定时咨询医疗专业人士
  • 未经适当验证,本系统不应用于实际的医疗诊断