pytorch_一个batch的tensor转化为图片查看

img = image_batch[0].mul(255).byte()
img = img.cpu().numpy().transpose((1, 2, 0))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# print("img",img)
cv2.imshow("12",img)
cv2.waitKey(0)

PyTorch Lightning Metric 是 PyTorch Lightning 中用于评估模型性能的一种工具。Metric 可以用于监控训练过程中的指标,并在每个 epoch 结束时输出结果。PyTorch Lightning Metric 提供了多种内置的评估指标,如 accuracy、precision、recall、F1 等,并且可以自定义评估指标。 使用 PyTorch Lightning Metric 的基本步骤如下: 1. 定义 Metric 类,继承自 `pl.metrics.Metric` 2. 在类中实现 `update` 方法,用于更新评估指标 3. 在类中实现 `compute` 方法,用于计算最终的评估结果 4. 在 LightningModule 中使用 `self.log()` 方法输出评估结果 例如,下面是一个计算 accuracy 的 Metric 类的示例代码: ```python import torch import pytorch_lightning as pl class Accuracy(pl.metrics.Metric): def __init__(self, dist_sync_on_step=False): super().__init__(dist_sync_on_step=dist_sync_on_step) self.add_state("correct", default=torch.tensor(0), dist_reduce_fx="sum") self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum") def update(self, preds, target): preds = torch.argmax(preds, dim=1) self.correct += torch.sum(preds == target) self.total += target.numel() def compute(self): return self.correct.float() / self.total ``` 在 LightningModule 中使用该 Metric 可以如下使用: ```python class MyModel(pl.LightningModule): def __init__(self): super().__init__() self.accuracy = Accuracy() def training_step(self, batch, batch_idx): ... self.accuracy(preds, target) ... def training_epoch_end(self, outputs): ... self.log('train_acc', self.accuracy.compute(), on_step=False, on_epoch=True) ... ``` 在每个 epoch 结束时,`self.accuracy.compute()` 方法将计算 accuracy 并返回最终的评估结果。`self.log()` 方法用于输出评估结果,其中 `on_epoch=True` 表示只在每个 epoch 结束时输出,而不是每个 batch 结束时都输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值