当前位置: 首页 > news >正文

PyTorch Lightning教程八:用模型预测,部署

关于Checkpoints的内容在教程2里已经有了详细的说明,在本节,需要用它来利用模型进行预测

加载checkpoint并预测

使用模型进行预测的最简单方法是使用LightningModule中的load_from_checkpoint加载权重。

model = LitModel.load_from_checkpoint("best_model.ckpt")
model.eval()
x = torch.randn(1, 64)with torch.no_grad():y_hat = model(x)

predict_step方法

加载检查点并进行预测仍然会在预测阶段的epoch留下许多boilerplate,LightningModule中的预测步骤删除了这个boilerplate 。

class MyModel(LightningModule):def predict_step(self, batch, batch_idx, dataloader_idx=0):return self(batch)

并将任何dataloader传递给Lightning Trainer

data_loader = DataLoader(...)
model = MyModel()
trainer = Trainer()
predictions = trainer.predict(model, data_loader)

预测逻辑

当需要向数据添加复杂的预处理或后处理时,使用predict_step方法。例如,这里我们使用Monte Carlo Dropout 进行预测

class LitMCdropoutModel(pl.LightningModule):def __init__(self, model, mc_iteration):super().__init__()self.model = modelself.dropout = nn.Dropout()self.mc_iteration = mc_iterationdef predict_step(self, batch, batch_idx):# enable Monte Carlo Dropoutself.dropout.train()# take average of `self.mc_iteration` iterationspred = [self.dropout(self.model(x)).unsqueeze(0) for _ in range(self.mc_iteration)]pred = torch.vstack(pred).mean(dim=0)return pred

启用分布式推理

通过使用Lightning中的predict_step,可以使用BasePredictionWriter进行分布式推理。

import torch
from lightning.pytorch.callbacks import BasePredictionWriterclass CustomWriter(BasePredictionWriter):def __init__(self, output_dir, write_interval):super().__init__(write_interval)self.output_dir = output_dirdef write_on_epoch_end(self, trainer, pl_module, predictions, batch_indices):# 在'output_dir'中创建N (num进程)个文件,每个文件都包含对其各自rank的预测torch.save(predictions, os.path.join(self.output_dir, f"predictions_{trainer.global_rank}.pt"))# 可以保存'batch_indices',以便从预测数据中获取有关数据索引的信息torch.save(batch_indices, os.path.join(self.output_dir, f"batch_indices_{trainer.global_rank}.pt"))# 可以设置writer_interval="batch"
pred_writer = CustomWriter(output_dir="pred_path", write_interval="epoch")
trainer = Trainer(accelerator="gpu", strategy="ddp", devices=8, callbacks=[pred_writer])
model = BoringModel()
trainer.predict(model, return_predictions=False)

也可以加载保存的checkpoint,把它当作一个普通的torch.nn.Module来使用。可以提取所有的torch.nn.Module,并在训练后使用LightningModule保存的checkpoint加载权重。建议从LightningModule的init和forward方法中复制明确的实现。

class Encoder(nn.Module):...class Decoder(nn.Module):...class AutoEncoderProd(nn.Module):def __init__(self):super().__init__()self.encoder = Encoder()self.decoder = Decoder()def forward(self, x):return self.encoder(x)class AutoEncoderSystem(LightningModule):def __init__(self):super().__init__()self.auto_encoder = AutoEncoderProd()def forward(self, x):return self.auto_encoder.encoder(x)def training_step(self, batch, batch_idx):x, y = batchy_hat = self.auto_encoder.encoder(x)y_hat = self.auto_encoder.decoder(y_hat)loss = ...return loss# 训练
trainer = Trainer(devices=2, accelerator="gpu", strategy="ddp")
model = AutoEncoderSystem()
trainer.fit(model, train_dataloader, val_dataloader)
trainer.save_checkpoint("best_model.ckpt")# 创建PyTorch模型并加载checkpoint权重
model = AutoEncoderProd()
checkpoint = torch.load("best_model.ckpt")
hyper_parameters = checkpoint["hyper_parameters"]# 恢复超参数
model = AutoEncoderProd(**hyper_parameters)model_weights = checkpoint["state_dict"]# 通过 dropping `auto_encoder.` 更新key值
for key in list(model_weights):model_weights[key.replace("auto_encoder.", "")] = model_weights.pop(key)model.load_state_dict(model_weights)
model.eval()
x = torch.randn(1, 64)with torch.no_grad():y_hat = model(x)
http://www.lryc.cn/news/119989.html

相关文章:

  • 桂林小程序https证书
  • html input 设置不允许修改
  • BI技巧丨利用Index计算半累计
  • 第三章:前端UI框架介绍
  • javaScript:文档流写入和元素写入
  • 【BI系统】选型常见问题解答二
  • docker版jxTMS使用指南:使用jxTMS采集数据之一
  • 【js】日期、时间正则匹配
  • 专利研读-SIMD系列-向量化引擎
  • C#--设计模式之单例模式
  • RWEQ风蚀方程模型与ArcGIS数据处理Python代码库添加结合理论研究和科研实践
  • 基于STM32微控制器的物联网(IoT)节点设计与实现
  • 篇二十一:中介者模式:解耦对象之间的交互
  • tomcat的多实例,动静分离(web服务基础结束)
  • LeetCode150道面试经典题--判断子序列(简单)
  • kubeadml 安装 k8s
  • 考研C语言进阶题库——更新16-20题
  • 【变形金刚01】attention和transformer所有信息
  • 面试热题(路径总和II)
  • 测试 tensorflow 1.x 的一个demo 01
  • 达蒙DM数据库使用经验
  • Redis—集群
  • 【C语言】数据在内存中的存储详解
  • PIC单片机配置字的设置
  • JavaWeb-Servlet服务连接器(一)
  • 新华三超融合态势感知标准版
  • AutoSAR系列讲解(深入篇)13.2-Mcal Port配置
  • Java旋转数组中的最小数字(图文详解版)
  • Android 13 Hotseat定制化修改——005 hotseat图标禁止形成文件夹
  • 插入、希尔、归并、快速排序(java实现)