Transformers 4.37 中文文档(五)(2)

简介: Transformers 4.37 中文文档(五)

Transformers 4.37 中文文档(五)(1)https://developer.aliyun.com/article/1565241


训练 DETR 模型

在前几节中,您已经完成了大部分繁重的工作,现在您已经准备好训练您的模型了!即使在调整大小后,此数据集中的图像仍然相当大。这意味着微调此模型将需要至少一个 GPU。

训练包括以下步骤:

  1. 使用与预处理中相同的检查点加载模型 AutoModelForObjectDetection。
  2. 在 TrainingArguments 中定义您的训练超参数。
  3. 将训练参数传递给 Trainer,以及模型、数据集、图像处理器和数据整理器。
  4. 调用 train()来微调您的模型。

在从用于预处理的相同检查点加载模型时,请记住传递您从数据集元数据中创建的label2id和id2label映射。此外,我们指定ignore_mismatched_sizes=True以用新的替换现有的分类头。

>>> from transformers import AutoModelForObjectDetection
>>> model = AutoModelForObjectDetection.from_pretrained(
...     checkpoint,
...     id2label=id2label,
...     label2id=label2id,
...     ignore_mismatched_sizes=True,
... )

在 TrainingArguments 中使用output_dir指定保存模型的位置,然后根据需要配置超参数。重要的是不要删除未使用的列,因为这将删除图像列。没有图像列,您无法创建pixel_values。因此,将remove_unused_columns设置为False。如果希望通过将其推送到 Hub 来共享您的模型,请将push_to_hub设置为True(您必须登录到 Hugging Face 才能上传您的模型)。

>>> from transformers import TrainingArguments
>>> training_args = TrainingArguments(
...     output_dir="detr-resnet-50_finetuned_cppe5",
...     per_device_train_batch_size=8,
...     num_train_epochs=10,
...     fp16=True,
...     save_steps=200,
...     logging_steps=50,
...     learning_rate=1e-5,
...     weight_decay=1e-4,
...     save_total_limit=2,
...     remove_unused_columns=False,
...     push_to_hub=True,
... )

最后,将所有内容汇总,并调用 train():

>>> from transformers import Trainer
>>> trainer = Trainer(
...     model=model,
...     args=training_args,
...     data_collator=collate_fn,
...     train_dataset=cppe5["train"],
...     tokenizer=image_processor,
... )
>>> trainer.train()

如果在training_args中将push_to_hub设置为True,则训练检查点将被推送到 Hugging Face Hub。在训练完成后,通过调用 push_to_hub()方法将最终模型也推送到 Hub。

>>> trainer.push_to_hub()

评估

目标检测模型通常使用一组COCO 风格指标进行评估。您可以使用现有的指标实现之一,但在这里,您将使用来自torchvision的指标来评估推送到 Hub 的最终模型。

要使用torchvision评估器,您需要准备一个真实的 COCO 数据集。构建 COCO 数据集的 API 要求数据以特定格式存储,因此您需要首先将图像和注释保存到磁盘上。就像您为训练准备数据时一样,来自cppe5["test"]的注释需要进行格式化。但是,图像应保持原样。

评估步骤需要一些工作,但可以分为三个主要步骤。首先,准备cppe5["test"]集:格式化注释并将数据保存到磁盘上。

>>> import json
>>> # format annotations the same as for training, no need for data augmentation
>>> def val_formatted_anns(image_id, objects):
...     annotations = []
...     for i in range(0, len(objects["id"])):
...         new_ann = {
...             "id": objects["id"][i],
...             "category_id": objects["category"][i],
...             "iscrowd": 0,
...             "image_id": image_id,
...             "area": objects["area"][i],
...             "bbox": objects["bbox"][i],
...         }
...         annotations.append(new_ann)
...     return annotations
>>> # Save images and annotations into the files torchvision.datasets.CocoDetection expects
>>> def save_cppe5_annotation_file_images(cppe5):
...     output_json = {}
...     path_output_cppe5 = f"{os.getcwd()}/cppe5/"
...     if not os.path.exists(path_output_cppe5):
...         os.makedirs(path_output_cppe5)
...     path_anno = os.path.join(path_output_cppe5, "cppe5_ann.json")
...     categories_json = [{"supercategory": "none", "id": id, "name": id2label[id]} for id in id2label]
...     output_json["images"] = []
...     output_json["annotations"] = []
...     for example in cppe5:
...         ann = val_formatted_anns(example["image_id"], example["objects"])
...         output_json["images"].append(
...             {
...                 "id": example["image_id"],
...                 "width": example["image"].width,
...                 "height": example["image"].height,
...                 "file_name": f"{example['image_id']}.png",
...             }
...         )
...         output_json["annotations"].extend(ann)
...     output_json["categories"] = categories_json
...     with open(path_anno, "w") as file:
...         json.dump(output_json, file, ensure_ascii=False, indent=4)
...     for im, img_id in zip(cppe5["image"], cppe5["image_id"]):
...         path_img = os.path.join(path_output_cppe5, f"{img_id}.png")
...         im.save(path_img)
...     return path_output_cppe5, path_anno

接下来,准备一个可以与cocoevaluator一起使用的CocoDetection类的实例。

>>> import torchvision
>>> class CocoDetection(torchvision.datasets.CocoDetection):
...     def __init__(self, img_folder, image_processor, ann_file):
...         super().__init__(img_folder, ann_file)
...         self.image_processor = image_processor
...     def __getitem__(self, idx):
...         # read in PIL image and target in COCO format
...         img, target = super(CocoDetection, self).__getitem__(idx)
...         # preprocess image and target: converting target to DETR format,
...         # resizing + normalization of both image and target)
...         image_id = self.ids[idx]
...         target = {"image_id": image_id, "annotations": target}
...         encoding = self.image_processor(images=img, annotations=target, return_tensors="pt")
...         pixel_values = encoding["pixel_values"].squeeze()  # remove batch dimension
...         target = encoding["labels"][0]  # remove batch dimension
...         return {"pixel_values": pixel_values, "labels": target}
>>> im_processor = AutoImageProcessor.from_pretrained("devonho/detr-resnet-50_finetuned_cppe5")
>>> path_output_cppe5, path_anno = save_cppe5_annotation_file_images(cppe5["test"])
>>> test_ds_coco_format = CocoDetection(path_output_cppe5, im_processor, path_anno)

最后,加载指标并运行评估。

>>> import evaluate
>>> from tqdm import tqdm
>>> model = AutoModelForObjectDetection.from_pretrained("devonho/detr-resnet-50_finetuned_cppe5")
>>> module = evaluate.load("ybelkada/cocoevaluate", coco=test_ds_coco_format.coco)
>>> val_dataloader = torch.utils.data.DataLoader(
...     test_ds_coco_format, batch_size=8, shuffle=False, num_workers=4, collate_fn=collate_fn
... )
>>> with torch.no_grad():
...     for idx, batch in enumerate(tqdm(val_dataloader)):
...         pixel_values = batch["pixel_values"]
...         pixel_mask = batch["pixel_mask"]
...         labels = [
...             {k: v for k, v in t.items()} for t in batch["labels"]
...         ]  # these are in DETR format, resized + normalized
...         # forward pass
...         outputs = model(pixel_values=pixel_values, pixel_mask=pixel_mask)
...         orig_target_sizes = torch.stack([target["orig_size"] for target in labels], dim=0)
...         results = im_processor.post_process(outputs, orig_target_sizes)  # convert outputs of model to Pascal VOC format (xmin, ymin, xmax, ymax)
...         module.add(prediction=results, reference=labels)
...         del batch
>>> results = module.compute()
>>> print(results)
Accumulating evaluation results...
DONE (t=0.08s).
IoU metric: bbox
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.352
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.681
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.292
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.168
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.208
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.429
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.274
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.484
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.501
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.191
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.323
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.590

通过调整 TrainingArguments 中的超参数,这些结果可以进一步改善。试一试吧!

推断

现在您已经微调了一个 DETR 模型,对其进行了评估,并将其上传到 Hugging Face  Hub,您可以将其用于推断。尝试使用您微调的模型进行推断的最简单方法是在 Pipeline  中使用它。使用您的模型实例化一个用于目标检测的流水线,并将图像传递给它:

>>> from transformers import pipeline
>>> import requests
>>> url = "https://i.imgur.com/2lnWoly.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> obj_detector = pipeline("object-detection", model="devonho/detr-resnet-50_finetuned_cppe5")
>>> obj_detector(image)

如果您愿意,也可以手动复制流水线的结果:

>>> image_processor = AutoImageProcessor.from_pretrained("devonho/detr-resnet-50_finetuned_cppe5")
>>> model = AutoModelForObjectDetection.from_pretrained("devonho/detr-resnet-50_finetuned_cppe5")
>>> with torch.no_grad():
...     inputs = image_processor(images=image, return_tensors="pt")
...     outputs = model(**inputs)
...     target_sizes = torch.tensor([image.size[::-1]])
...     results = image_processor.post_process_object_detection(outputs, threshold=0.5, target_sizes=target_sizes)[0]
>>> for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
...     box = [round(i, 2) for i in box.tolist()]
...     print(
...         f"Detected {model.config.id2label[label.item()]} with confidence "
...         f"{round(score.item(), 3)} at location {box}"
...     )
Detected Coverall with confidence 0.566 at location [1215.32, 147.38, 4401.81, 3227.08]
Detected Mask with confidence 0.584 at location [2449.06, 823.19, 3256.43, 1413.9]

让我们绘制结果:

>>> draw = ImageDraw.Draw(image)
>>> for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
...     box = [round(i, 2) for i in box.tolist()]
...     x, y, x2, y2 = tuple(box)
...     draw.rectangle((x, y, x2, y2), outline="red", width=1)
...     draw.text((x, y), model.config.id2label[label.item()], fill="white")
>>> image

在一张新图片上的目标检测结果


Transformers 4.37 中文文档(五)(3)https://developer.aliyun.com/article/1565243

相关文章
|
机器学习/深度学习 自然语言处理 PyTorch
VLLM (Very Large Language Model)
VLLM (Very Large Language Model) 是一种大型语言模型,通常具有数十亿或数万亿个参数,用于处理自然语言文本。VLLM 可以通过预训练和微调来执行各种任务,如文本分类、机器翻译、情感分析、问答等。
1795 1
|
NoSQL Java Redis
SpringBoot2.0整合Redis高可用之Sentinel哨兵
本篇博文分享的是一主二从三哨兵模式。至于为什么用三个哨兵,同第一段。本文是模拟环境,都是一个服务器上面。
2625 0
|
10月前
|
人工智能 JavaScript 机器人
保姆级教程:OpenClaw(Clawdbot)阿里云+Windows本地部署,集成钉钉 AI助手 7×24小时可用
OpenClaw(曾用名Clawdbot/Moltbot)是2026年主流开源AI智能体框架,支持私有化部署、多渠道接入与自动化任务执行。本文基于2026年最新实操流程,提供阿里云服务器部署、Windows本地部署两套方案,搭配钉钉集成完整步骤与可直接复制的代码命令,帮助用户从零完成部署与集成,实现钉钉内AI助手7×24小时可用。
2547 2
|
4月前
|
弹性计算 缓存 运维
阿里云服务器贵不贵?一文看懂阿里云服务器多少钱:配置价格表、优惠福利、计费选型避坑指南
很多个人开发者、学生、中小企业在上云初期,都会关心云服务器的实际采购成本。阿里云服务器主要分为轻量应用服务器与云服务器ECS两大产品线,覆盖个人入门学习、开发者测试、中小型企业生产、高性能计算等不同层级业务场景。整体价格跨度很大,入门档位低价即可入手,高性能实例则可达数万元每年,整体呈现“入门普惠、中阶稳定、高阶弹性”的特征。新用户拥有力度可观的折扣,部分实例支持老用户续费同价,长期使用成本可控。本文将从产品定位、配置价格明细、性价比分析、各类人群优惠政策、三种计费模式、命令行实操、省钱技巧、常见避坑点多维度完整讲解,附带可直接复制的CLI命令,帮助使用者合理选型,规避多花冤枉钱的问题。
521 0
|
5月前
|
Cloud Native 安全 数据库
云原生架构实战:美业 SaaS 走向私有化,多租户架构与数据隔离深度实践
本文介绍青海青帝科技基于阿里云原生能力,为医美、高端SPA等连锁业态构建高安全私有化多租户中台:通过VPC+独立Schema实现物理级数据隔离,行级加密与动态脱敏保障“可用不可见”,乐观锁确保预付资金强一致性。(239字)
470 1
|
人工智能 程序员 API
Motia:程序员福音!AI智能体三语言混编,零基础秒级部署
Motia 是一款专为软件工程师设计的 AI Agent 开发框架,支持多种编程语言,提供零基础设施部署、模块化设计和内置可观测性功能,帮助开发者快速构建和部署智能体。
1501 15
Motia:程序员福音!AI智能体三语言混编,零基础秒级部署
|
10月前
|
人工智能 JavaScript API
零门槛部署 Windows 系统本地及云上 AI 助手:OpenClaw(原 Clawdbot/Moltbot)保姆级教程
2026年初,一款名为OpenClaw的AI助手在开发者社区迅速走红,它前身为Clawdbot、Moltbot,经过品牌整合后统一命名为“OpenClaw”。这款AI助手并非传统意义上的聊天工具,而是具备“动手执行”能力的智能体——既能读写本地文件、执行代码、操控命令行,又能联网搜索、分析网页内容,还可接入Qwen、OpenAI等云端API或利用本地GPU运行模型,像“私人AI员工”般处理各类事务。
7915 9
|
缓存 运维 监控
vLLM推理加速指南:7个技巧让QPS提升30-60%
GPU资源有限,提升推理效率需多管齐下。本文分享vLLM实战调优七招:请求塑形、KV缓存复用、推测解码、量化、并行策略、准入控制与预热监控。结合代码与数据,助你最大化吞吐、降低延迟,实现高QPS稳定服务。
3111 7
vLLM推理加速指南:7个技巧让QPS提升30-60%
|
SQL Java 数据库连接
自定义HikariCP连接池
自定义HikariCP连接池
2770 0
|
人工智能 自然语言处理 Swift
"轻量级微调推理框架SWIFT:大模型时代的速度革命,让你秒变AI部署高手!"
【8月更文挑战第17天】随着AI技术的发展,大模型如GPT-3和BERT引领风潮,但其部署与推理速度面临挑战。为此,魔搭社区推出了SWIFT(Simple Weight-Integrated Fine-Tuning)框架,它采用轻量级微调技术,实现模型参数压缩与加速,确保大模型能在移动端和边缘设备上高效运行。SWIFT具备四大特点:创新微调方法减少训练参数;内置优化策略提高推理速度;跨平台支持便于部署;兼容主流预训练模型。通过示例可见,从加载预训练模型到模型的微调、评估及导出,SWIFT简化了工作流程,降低了大模型的应用门槛,促进了AI技术的实际应用。
2227 4