[{"data":1,"prerenderedAt":523},["ShallowReactive",2],{"content-query-Vgl60zIKV5":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"body":13,"_type":517,"_id":518,"_source":519,"_file":520,"_stem":521,"_extension":522},"/technology-blogs/zh/3820","zh",false,"","BiT模型论文解读，并基于MindSpore NLP推理复现","预训练模型已经成为下游任务迁移学习的标准做法","2025-08-27","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/08/29/2323087dcc224f61947be44e70e4ff22.png","technology-blogs",{"type":14,"children":15,"toc":514},"root",[16,24,30,35,40,57,65,70,78,86,91,98,108,113,121,126,134,139,154,159,169,174,179,184,189,194,199,204,209,214,224,229,234,249,258,263,270,279,284,291,299,304,311,326,335,361,370,375,384,417,422,437,442,452,457,465,470,477,482,497,501],{"type":17,"tag":18,"props":19,"children":21},"element","h1",{"id":20},"bit模型论文解读并基于mindspore-nlp推理复现",[22],{"type":23,"value":8},"text",{"type":17,"tag":25,"props":26,"children":27},"p",{},[28],{"type":23,"value":29},"作者：songchenyang 来源：开源实习",{"type":17,"tag":25,"props":31,"children":32},{},[33],{"type":23,"value":34},"在计算机视觉领域，预训练模型（如在ImageNet上训练的卷积神经网络）已经成为下游任务迁移学习的标准做法。然而，随着数据集规模和模型复杂度的增加，如何高效地利用大规模数据预训练模型，并在小规模或多样化的下游任务中实现高效迁移，成为一个亟待解决的问题。",{"type":17,"tag":25,"props":36,"children":37},{},[38],{"type":23,"value":39},"《Big Transfer（BiT）: General Visual Representation Learning》是在2019年由 Alexander Kolesnikov, Lucas Beyer, Xiaohua Zhai 等人提出的一种简单而高效的预训练方法，通过在大规模数据集上训练大型模型（Big Models），并结合迁移学习技巧（Transfer Techniques），显著提升下游任务的表现，同时保持方法的通用性和易用性。BiT的核心思想是规模为王：更大的模型和更大的数据集能够学习到更通用的视觉表征，而不需要复杂的训练策略或任务特定的调整。这种方法与当时流行的复杂预训练方法（如自监督学习或多任务学习）形成对比，强调简单性和可扩展性。",{"type":17,"tag":18,"props":41,"children":43},{"id":42},"_01-论文创新点",[44,50,52],{"type":17,"tag":45,"props":46,"children":47},"strong",{},[48],{"type":23,"value":49},"# 01",{"type":23,"value":51}," ",{"type":17,"tag":45,"props":53,"children":54},{},[55],{"type":23,"value":56},"论文创新点",{"type":17,"tag":25,"props":58,"children":59},{},[60],{"type":17,"tag":45,"props":61,"children":62},{},[63],{"type":23,"value":64},"1、大规模预训练的简单框架",{"type":17,"tag":25,"props":66,"children":67},{},[68],{"type":23,"value":69},"BiT提出了一种基于监督学习的预训练方法，利用大规模数据集（如ImageNet-21k和JFT-300M）和大型模型（如ResNet变体），通过标准的分类任务学习通用视觉表征。这种方法与复杂的自监督学习方法（如SimCLR、MoCo）形成鲜明对比。BiT仅依赖监督信号，却依然取得了卓越的性能，证明了在大规模数据场景下，简单的监督学习依然可以实现强大的表征能力。",{"type":17,"tag":25,"props":71,"children":72},{},[73],{"type":17,"tag":74,"props":75,"children":77},"img",{"alt":7,"src":76},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/08/29/945a3c2b0e194d46911b1aa0c3fe7de1.png",[],{"type":17,"tag":25,"props":79,"children":80},{},[81],{"type":17,"tag":45,"props":82,"children":83},{},[84],{"type":23,"value":85},"2、模型规模与数据规模的协同效应",{"type":17,"tag":25,"props":87,"children":88},{},[89],{"type":23,"value":90},"论文验证了模型规模（如ResNet-152x4）和数据集规模（如JFT-300M，3亿图像）的协同作用能够显著提升表征能力。BiT表明，单纯增加数据或模型规模可能不足以优化性能，二者需同步扩展以实现最佳效果。这种“双重扩展”的策略弥补了对复杂训练技巧的需求。",{"type":17,"tag":25,"props":92,"children":93},{},[94],{"type":17,"tag":74,"props":95,"children":97},{"alt":7,"src":96},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/08/29/648ca97feeb347128a9d287f77c963b7.png",[],{"type":17,"tag":25,"props":99,"children":100},{},[101,103],{"type":23,"value":102},"**3、**",{"type":17,"tag":45,"props":104,"children":105},{},[106],{"type":23,"value":107},"标准化迁移学习协议（BiT-HyperRule）",{"type":17,"tag":25,"props":109,"children":110},{},[111],{"type":23,"value":112},"BiT提出了一套简单高效的迁移学习规则（BiT-HyperRule），包括微调策略和超参数选择指南，避免了为每个下游任务进行昂贵的超参数搜索。该规则根据任务的图像分辨率和数据量动态调整关键超参数（如学习率、训练步数、分辨率），使迁移过程标准化且易于实施。",{"type":17,"tag":25,"props":114,"children":115},{},[116],{"type":17,"tag":45,"props":117,"children":118},{},[119],{"type":23,"value":120},"4、对小数据集的高效迁移",{"type":17,"tag":25,"props":122,"children":123},{},[124],{"type":23,"value":125},"BiT在大规模预训练后，即使在极小数据量的下游任务（如每个类别仅1-10张图像）上也能表现出色，解决了传统预训练模型在小数据集上易过拟合的问题。这种能力在数据稀缺场景（如医疗影像、特定领域分类）中具有重要应用价值。",{"type":17,"tag":25,"props":127,"children":128},{},[129],{"type":17,"tag":45,"props":130,"children":131},{},[132],{"type":23,"value":133},"5、去除了不必要的复杂性",{"type":17,"tag":25,"props":135,"children":136},{},[137],{"type":23,"value":138},"BiT避免使用当时流行的正则化技术（如Dropout、Mixup）或复杂损失函数，证明了简单的预训练+微调流程足以应对多样化的视觉任务。通过精心选择少量关键组件（如Group Normalization和Weight Standardization），BiT在保持简单性的同时实现了高性能。",{"type":17,"tag":18,"props":140,"children":142},{"id":141},"_02-方法细节",[143,148,149],{"type":17,"tag":45,"props":144,"children":145},{},[146],{"type":23,"value":147},"# 02",{"type":23,"value":51},{"type":17,"tag":45,"props":150,"children":151},{},[152],{"type":23,"value":153},"方法细节",{"type":17,"tag":25,"props":155,"children":156},{},[157],{"type":23,"value":158},"BiT的实现分为两个主要阶段：上游预训练（Upstream Pre-Training）和下游迁移学习（Transfer to Downstream Tasks）。",{"type":17,"tag":25,"props":160,"children":161},{},[162,164],{"type":23,"value":163},"**1、**",{"type":17,"tag":45,"props":165,"children":166},{},[167],{"type":23,"value":168},"预训练阶段",{"type":17,"tag":25,"props":170,"children":171},{},[172],{"type":23,"value":173},"BiT 基于 ResNet 架构，提出了三种不同规模的变体，以适应不同计算资源和任务需求：",{"type":17,"tag":25,"props":175,"children":176},{},[177],{"type":23,"value":178},"**BiT-S：**小型模型，例如 ResNet-50x1，训练于 ILSVRC-2012 数据集，包含约 130 万张图像。",{"type":17,"tag":25,"props":180,"children":181},{},[182],{"type":23,"value":183},"**BiT-M：**中型模型，例如 ResNet-101x1，训练于 ImageNet-21k 数据集，包含约 1400 万张图像。",{"type":17,"tag":25,"props":185,"children":186},{},[187],{"type":23,"value":188},"**BiT-L：**大型模型，例如 ResNet-152x4（宽度乘以 4 倍），训练于 JFT-300M 数据集，包含约 3 亿张图像。",{"type":17,"tag":25,"props":190,"children":191},{},[192],{"type":23,"value":193},"这些模型的深度和宽度逐渐增加，以充分利用大规模数据集的潜力。",{"type":17,"tag":25,"props":195,"children":196},{},[197],{"type":23,"value":198},"BiT 使用了三个不同规模的数据集进行预训练：",{"type":17,"tag":25,"props":200,"children":201},{},[202],{"type":23,"value":203},"**ILSVRC-2012：**包含 1000 类，约 130 万张图像，作为基准数据集。",{"type":17,"tag":25,"props":205,"children":206},{},[207],{"type":23,"value":208},"**ImageNet-21k：**包含 2.1 万类，约 1400 万张图像，数据量更大且类别更丰富。",{"type":17,"tag":25,"props":210,"children":211},{},[212],{"type":23,"value":213},"**JFT-300M：**谷歌内部数据集，包含 3 亿张图像和 1.8 万类，带有噪声标签，是超大规模数据的代表。",{"type":17,"tag":25,"props":215,"children":216},{},[217,219],{"type":23,"value":218},"**2、**",{"type":17,"tag":45,"props":220,"children":221},{},[222],{"type":23,"value":223},"迁移学习阶段",{"type":17,"tag":25,"props":225,"children":226},{},[227],{"type":23,"value":228},"微调策略：将预训练模型的卷积层（特征提取器）迁移到下游任务，仅替换最后一层全连接层以适应目标任务的类别数。冻结卷积层权重，仅训练全连接层（适用于小数据集）。对于大数据集，则对整个模型进行端到端微调。",{"type":17,"tag":25,"props":230,"children":231},{},[232],{"type":23,"value":233},"BiT-HyperRule：BiT 提出了一套简单的迁移学习规则（BiT-HyperRule），包括预训练后的微调策略和超参数选择指南：仅使用基本的翻转和裁剪，不依赖复杂增强（如 Mixup）。小于96 × 96的分辨率，先将图片resize到160 × 160 ，再随机剪切出128 × 128的方框；对于大于96 × 96 的分辨率，先将图片resize到512 × 512 ，再随机剪切出480 × 480 的方框。根据任务数据量动态调整，小数据集使用较小的学习率（如 0.003），大数据集使用较大的学习率（如 0.01）。小数据集训练较短（如 500 步），大数据集训练较长（如 20 个 epoch）。移除 Dropout 等技巧，仅依赖 L2 正则化。",{"type":17,"tag":18,"props":235,"children":237},{"id":236},"_03-实验结果与分析",[238,243,244],{"type":17,"tag":45,"props":239,"children":240},{},[241],{"type":23,"value":242},"# 03",{"type":23,"value":51},{"type":17,"tag":45,"props":245,"children":246},{},[247],{"type":23,"value":248},"实验结果与分析",{"type":17,"tag":25,"props":250,"children":251},{},[252,253],{"type":23,"value":163},{"type":17,"tag":45,"props":254,"children":255},{},[256],{"type":23,"value":257},"单一模型与单一超参数设置的迁移性能",{"type":17,"tag":25,"props":259,"children":260},{},[261],{"type":23,"value":262},"BiT 的核心思想是通过大规模预训练和简单的迁移学习策略，在多种下游任务上实现卓越的性能。BiT-L 模型在多个数据集上使用单一模型架构和每个任务的单一超参数设置（BiT-HyperRule）进行微调，取得了显著的成果。这些结果不仅超越了当时的通用 SOTA（State-of-the-Art）模型，还在许多情况下优于专门针对特定任务训练的专家模型。",{"type":17,"tag":25,"props":264,"children":265},{},[266],{"type":17,"tag":74,"props":267,"children":269},{"alt":7,"src":268},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/08/29/722d114e6e9846f4bb53f770a1250c89.png",[],{"type":17,"tag":25,"props":271,"children":272},{},[273,274],{"type":23,"value":218},{"type":17,"tag":45,"props":275,"children":276},{},[277],{"type":23,"value":278},"在 ImageNet-21k 上的预训练改进",{"type":17,"tag":25,"props":280,"children":281},{},[282],{"type":23,"value":283},"BiT还展示了在公共 ImageNet-21k 数据集上进行预训练的优势。与传统的 ILSVRC-2012 数据集相比，ImageNet-21k 的规模更大，包含约 1400 万张图像和 2.1 万个类别。BiT 在 ImageNet-21k 上的预训练显著提高了模型的性能。",{"type":17,"tag":25,"props":285,"children":286},{},[287],{"type":17,"tag":74,"props":288,"children":290},{"alt":7,"src":289},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/08/29/09d4ac2080f844d495281dbc39a2769b.png",[],{"type":17,"tag":25,"props":292,"children":293},{},[294],{"type":17,"tag":45,"props":295,"children":296},{},[297],{"type":23,"value":298},"3、在物体检测任务中的应用",{"type":17,"tag":25,"props":300,"children":301},{},[302],{"type":23,"value":303},"BiT 的迁移学习策略不仅限于图像分类任务，还在物体检测领域取得了显著成果。在 COCO-2017 数据集上，BiT 作为 RetinaNet 检测器的主干网络，进行了微调。结果显示，BiT 模型在物体检测任务上的表现优于标准的 ImageNet 预训练模型。",{"type":17,"tag":25,"props":305,"children":306},{},[307],{"type":17,"tag":74,"props":308,"children":310},{"alt":7,"src":309},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/08/29/10286bbc54af4f5d975ef8cbe24961fd.png",[],{"type":17,"tag":18,"props":312,"children":314},{"id":313},"_04-创新点优势分析",[315,320,321],{"type":17,"tag":45,"props":316,"children":317},{},[318],{"type":23,"value":319},"# 04",{"type":23,"value":51},{"type":17,"tag":45,"props":322,"children":323},{},[324],{"type":23,"value":325},"创新点优势分析",{"type":17,"tag":25,"props":327,"children":328},{},[329,330],{"type":23,"value":163},{"type":17,"tag":45,"props":331,"children":332},{},[333],{"type":23,"value":334},"大规模预训练的高效性",{"type":17,"tag":25,"props":336,"children":337},{},[338,340,345,347,352,354,359],{"type":23,"value":339},"BiT 在大规模数据集（如 ImageNet-21k 和 JFT-300M）上进行预训练，显著提升了模型的泛化能力和性能。这种大规模预训练不仅优于传统的 ILSVRC-2012 数据集，还在下游任务的少样本学习和小数据集任务中表现出色。例如，BiT-L 在 ILSVRC-2012 上达到了 ",{"type":17,"tag":45,"props":341,"children":342},{},[343],{"type":23,"value":344},"87.5%",{"type":23,"value":346}," 的 top-1 准确率，而在 CIFAR-10 上，即使每个类别只有 ",{"type":17,"tag":45,"props":348,"children":349},{},[350],{"type":23,"value":351},"10 个样本",{"type":23,"value":353},"，也能达到 ",{"type":17,"tag":45,"props":355,"children":356},{},[357],{"type":23,"value":358},"97.0%",{"type":23,"value":360}," 的准确率。这种能力使得 BiT 在标注数据稀缺的任务中具有显著优势。",{"type":17,"tag":25,"props":362,"children":363},{},[364,365],{"type":23,"value":218},{"type":17,"tag":45,"props":366,"children":367},{},[368],{"type":23,"value":369},"简化的迁移学习策略",{"type":17,"tag":25,"props":371,"children":372},{},[373],{"type":23,"value":374},"BiT 提出了 BiT-HyperRule，这是一种简化的超参数调整策略，避免了对每个新任务进行繁琐的超参数搜索。BiT-HyperRule 根据任务的图像分辨率和数据点数量动态调整超参数，包括学习率、训练计划长度和是否使用 MixUp 正则化。这种策略不仅提高了迁移学习的效率，还确保了模型在不同任务上的高性能，减少了手动调参的工作量。",{"type":17,"tag":25,"props":376,"children":377},{},[378,379],{"type":23,"value":102},{"type":17,"tag":45,"props":380,"children":381},{},[382],{"type":23,"value":383},"广泛的适用性和高性能",{"type":17,"tag":25,"props":385,"children":386},{},[387,389,394,396,401,403,408,410,415],{"type":23,"value":388},"BiT 在多种数据规模和复杂度的任务上表现出色，从每个类别只有 ",{"type":17,"tag":45,"props":390,"children":391},{},[392],{"type":23,"value":393},"1 个样本",{"type":23,"value":395}," 的小数据集到包含 ",{"type":17,"tag":45,"props":397,"children":398},{},[399],{"type":23,"value":400},"数百万样本",{"type":23,"value":402}," 的大数据集。例如，在 19 项任务的视觉任务适应基准（VTAB）上，BiT 的平均准确率达到了 ",{"type":17,"tag":45,"props":404,"children":405},{},[406],{"type":23,"value":407},"76.3%",{"type":23,"value":409},"。此外，BiT 在物体检测任务中也表现出色，使用 BiT-L 预训练的 RetinaNet 在 COCO-2017 验证集上达到了 ",{"type":17,"tag":45,"props":411,"children":412},{},[413],{"type":23,"value":414},"43.8%",{"type":23,"value":416}," 的平均精度（AP），显著优于使用 ILSVRC-2012 预训练的模型。",{"type":17,"tag":25,"props":418,"children":419},{},[420],{"type":23,"value":421},"BiT 通过大规模预训练和简化的迁移学习策略，显著提升了模型的泛化能力和适用性。其在少样本学习和小数据集任务中的卓越表现，使其在标注数据稀缺的任务中具有重要应用价值。BiT 的创新点不仅在于大规模数据集的预训练，还在于通过 BiT-HyperRule 自动化超参数调整，显著提升了迁移学习的效率和适用性。",{"type":17,"tag":18,"props":423,"children":425},{"id":424},"_05-使用mindspore-nlp进行模型评估",[426,431,432],{"type":17,"tag":45,"props":427,"children":428},{},[429],{"type":23,"value":430},"# 05",{"type":23,"value":51},{"type":17,"tag":45,"props":433,"children":434},{},[435],{"type":23,"value":436},"使用MindSpore NLP进行****模型评估",{"type":17,"tag":25,"props":438,"children":439},{},[440],{"type":23,"value":441},"实现代码如下：",{"type":17,"tag":443,"props":444,"children":446},"pre",{"code":445},"\nimport argparse\nimport os\nfrom tqdm import tqdm\nimport numpy as np\nimport mindspore\nfrom mindspore import nn, ops, Tensor\nfrom datasets import load_dataset\nfrom mindnlp.transformers import BitForImageClassification, BitConfig\n\nos.environ['HF_ENDPOINT'] = 'https://hf-mirror.com '\n\ndef parse_arguments():\n    parser = argparse.ArgumentParser(description=\"MindNLP BiT 评估示例\")\n    parser.add_argument(\"--model_name\", type=str, default=\"google/bit-50\", help=\"模型名称\")\n    parser.add_argument(\"--dataset_name\", type=str, default=\"cifar10\", help=\"数据集名称\")\n    parser.add_argument(\"--batch_size\", type=int, default=8, help=\"批次大小\")\n    return parser.parse_args()\n\ndef load_data(dataset_name, batch_size):\n    dataset = load_dataset(dataset_name, split='test')\n    def preprocess(batch):\n        images = np.stack([np.array(img) for img in batch['img']]) / 255.0 \n        images = images.transpose(0, 3, 1, 2)  \n        labels = np.array(batch['label'])  \n        return {\"images\": images, \"labels\": labels}\n   \n    # 应用预处理并批处理数据集\n    dataset = dataset.map(preprocess, batched=True)\n    dataset = dataset.batch(batch_size)\n    return dataset\n\ndef main():\n    args = parse_arguments()\n    dataset = load_data(args.dataset_name, args.batch_size)\n    # 初始化 BiT 配置\n    config = BitConfig.from_pretrained(args.model_name, num_labels=10)  \n    # 初始化模型\n    model = BitForImageClassification.from_pretrained(\n        args.model_name, config=config, ignore_mismatched_sizes=True\n    )\n    \n    # 评估模式\n    try:\n        model.eval()\n        print(\"Model switched to evaluation mode successfully.\")\n    except AttributeError:\n        print(\"Warning: Model does not support eval(). Proceeding without mode switch.\")\n   \n    # 定义损失函数\n    loss_fn = nn.CrossEntropyLoss()\n    \n    # 定义评估步骤\n    def eval_step(model, images, labels):\n        # 数组转换\n        images = Tensor(images, dtype=mindspore.float32)\n        labels = Tensor(labels, dtype=mindspore.int32)\n        outputs = model(images)  \n        logits = outputs.logits\n        preds = ops.argmax(logits, dim=1) \n        # 计算损失\n        loss = loss_fn(logits, labels)\n        # 计算准确率\n        equals = ops.cast(ops.equal(preds, labels), mindspore.float32)\n        accuracy = ops.reduce_mean(equals)\n        return loss, accuracy\n    \n    # 评估模型\n    total_loss = 0.0\n    total_accuracy = 0.0\n    num_batches = 0\n    for batch in tqdm(dataset, desc=\"评估中\"):\n        if isinstance(batch, dict):\n            images, labels = batch['images'], batch['labels']\n        else:\n            raise ValueError(\"Batch is not in expected dictionary format\")\n        loss, accuracy = eval_step(model, images, labels)\n        total_loss += loss.asnumpy()  \n        total_accuracy += accuracy.asnumpy() \n        num_batches += 1\n   \n    avg_loss = total_loss / num_batches\n    avg_accuracy = total_accuracy / num_batches\n    print(f\"平均损失: {avg_loss}\")\n    print(f\"测试准确率: {avg_accuracy}\")\n\nif __name__ == \"__main__\":\n    main()\n",[447],{"type":17,"tag":448,"props":449,"children":450},"code",{"__ignoreMap":7},[451],{"type":23,"value":445},{"type":17,"tag":25,"props":453,"children":454},{},[455],{"type":23,"value":456},"论文在该模型测试都应用于下游微调后的任务，这里仅比较预训练模型的loss值,选用cifar10数据集:",{"type":17,"tag":25,"props":458,"children":459},{},[460],{"type":17,"tag":45,"props":461,"children":462},{},[463],{"type":23,"value":464},"推理结果",{"type":17,"tag":25,"props":466,"children":467},{},[468],{"type":23,"value":469},"基于 MindSpore NLP 框架，在 ArXiv、PubMed 和 Big Patent 长文本数据集上使用 BigBird-Pegasus 预训练模型进行推理，计算平均 ROUGE 得分，结果汇总如下表所示：",{"type":17,"tag":25,"props":471,"children":472},{},[473],{"type":17,"tag":74,"props":474,"children":476},{"alt":7,"src":475},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/08/29/c8b38afc9e19405890fea9269602385a.png",[],{"type":17,"tag":25,"props":478,"children":479},{},[480],{"type":23,"value":481},"可以看出MindSpore NLP框架实现性能和Transformer实现相近。使用MindSpore NLP可以帮助我们更方便快捷地构建和训练模型，也推荐大家对BiT模型进行微调和推理验证。",{"type":17,"tag":18,"props":483,"children":485},{"id":484},"_06-总结",[486,491,492],{"type":17,"tag":45,"props":487,"children":488},{},[489],{"type":23,"value":490},"# 06",{"type":23,"value":51},{"type":17,"tag":45,"props":493,"children":494},{},[495],{"type":23,"value":496},"总结",{"type":17,"tag":25,"props":498,"children":499},{},[500],{"type":23,"value":421},{"type":17,"tag":25,"props":502,"children":503},{},[504,506],{"type":23,"value":505},"参考链接:",{"type":17,"tag":507,"props":508,"children":512},"a",{"href":509,"rel":510},"https://arxiv.org/abs/1912.1137",[511],"nofollow",[513],{"type":23,"value":509},{"title":7,"searchDepth":515,"depth":515,"links":516},4,[],"markdown","content:technology-blogs:zh:3820.md","content","technology-blogs/zh/3820.md","technology-blogs/zh/3820","md",1776506135737]