脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pytorch plt.savefig()的用法及保存路径

pytorch plt.savefig()的用法及保存路径

2022-09-05 11:13因为简单,所以快乐 Python

这篇文章主要给大家介绍了关于pytorch plt.savefig()的用法及保存路径的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

图像有时候比数据更能满足人们的视觉需求

Pytorch中保存图片的方式

pytorch下保存图像有很多种方法,但是这些基本上都是基于图像处理的,将图像的像素指定一定的维度 ,方法如下:

1、tensor直接保存

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import torch
from torchvision import utils as vutils
 
 
def save_image_tensor(input_tensor: torch.Tensor, filename):
    """
    将tensor保存为图片
    :param input_tensor: 要保存的tensor
    :param filename: 保存的文件名
    """
    assert (len(input_tensor.shape) == 4 and input_tensor.shape[0] == 1)
    # 复制一份
    input_tensor = input_tensor.clone().detach()
    # 到cpu
    input_tensor = input_tensor.to(torch.device('cpu'))
    # 反归一化
    # input_tensor = unnormalize(input_tensor)
    vutils.save_image(input_tensor, filename)

2、tensor转cv2保存

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import torch
import cv2
 
 
def save_image_tensor2cv2(input_tensor: torch.Tensor, filename):
    """
    将tensor保存为cv2格式
    :param input_tensor: 要保存的tensor
    :param filename: 保存的文件名
    """
    assert (len(input_tensor.shape) == 4 and input_tensor.shape[0] == 1)
    # 复制一份
    input_tensor = input_tensor.clone().detach()
    # 到cpu
    input_tensor = input_tensor.to(torch.device('cpu'))
    # 反归一化
    # input_tensor = unnormalize(input_tensor)
    # 去掉批次维度
    input_tensor = input_tensor.squeeze()
    # 从[0,1]转化为[0,255],再从CHW转为HWC,最后转为cv2
    input_tensor = input_tensor.mul_(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).type(torch.uint8).numpy()
    # RGB转BRG
    input_tensor = cv2.cvtColor(input_tensor, cv2.COLOR_RGB2BGR)
    cv2.imwrite(filename, input_tensor)

3、tensor转pillow保存

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def save_image_tensor2pillow(input_tensor: torch.Tensor, filename):
    """
    将tensor保存为pillow
    :param input_tensor: 要保存的tensor
    :param filename: 保存的文件名
    """
    assert (len(input_tensor.shape) == 4 and input_tensor.shape[0] == 1)
    # 复制一份
    input_tensor = input_tensor.clone().detach()
    # 到cpu
    input_tensor = input_tensor.to(torch.device('cpu'))
    # 反归一化
    # input_tensor = unnormalize(input_tensor)
    # 去掉批次维度
    input_tensor = input_tensor.squeeze()
    # 从[0,1]转化为[0,255],再从CHW转为HWC,最后转为numpy
    input_tensor = input_tensor.mul_(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).type(torch.uint8).numpy()
    # 转成pillow
    im = Image.fromarray(input_tensor)
    im.save(filename)

 主要是写一些函数来保存图片;

另外,pytorch中有很多可以直接保存图片的语句

?
1
save_image(fake_images, './img/fake_images-{}.png'.format(epoch + 1))

此语句同样需要转化像素。

那么如果

我只需要打开一个视窗,观察训练过程中图像的变化,我对图像像素保存没有什么需求,只是保存一个视窗,那么我需要的保存图像的函数仅仅是一个

plt.savefig

plt.savefig的用法以及保存的路径,及训练过程中不会被覆盖掉,可以上代码供大家参考

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if epoch % 10== 0:
    plt.title('ber:{:.3f},a: {:.3f},b:{:.3f},snr: {:.3f}'.format(
    error_rate, a, b,M
    ))
    plt.plot(r3)  # 绘制波形
 
    # save_image(r3, './img/fake_images-{}.png'.format(epoch + 1))
    # print(type(r3))
    # plt.draw()
    plt.draw()
    plt.savefig('./img/pic-{}.png'.format(epoch + 1))
    plt.pause(1)
 
    plt.close(fig1)

大功告成,可以看看保存后的图片

pytorch plt.savefig()的用法及保存路径

 已经都整整齐齐的在我的保存路径下了。

总结

到此这篇关于pytorch plt.savefig()用法及保存路径的文章就介绍到这了,更多相关plt.savefig()用法及路径内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/xzm961226xzm/article/details/120951317

延伸 · 阅读

精彩推荐
  • Pythonpython Django的显示个人信息详解

    python Django的显示个人信息详解

    这篇文章主要介绍了在Python的Django的显示个人信息方法,需要的朋友可以参考下,希望能够给你带来帮助...

    feng*3892022-01-10
  • Python在Django框架中编写Context处理器的方法

    在Django框架中编写Context处理器的方法

    这篇文章主要介绍了在Django框架中编写Context处理器的方法,Django是重多高人气Python框架中最为著名的一个,需要的朋友可以参考下...

    pythoner5442020-07-25
  • Python由浅入深学习TensorFlow MNIST 数据集

    由浅入深学习TensorFlow MNIST 数据集

    这篇文章主要由浅入深学习的讲解TensorFlow MNIST 数据集,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考...

    我是小白呀8612022-01-01
  • Python教你学会使用Python正则表达式

    教你学会使用Python正则表达式

    正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。 Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达...

    米乐果果3922020-12-05
  • PythonPython必须了解的35个关键词

    Python必须了解的35个关键词

    这篇文章主要介绍了Python必须了解的35个关键词,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下 ...

    吃着东西不想停5522020-07-16
  • Pythonpython thrift搭建服务端和客户端测试程序

    python thrift搭建服务端和客户端测试程序

    这篇文章主要为大家详细介绍了python thrift搭建服务端和客户端测试程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    magedu-Derek6012021-01-05
  • Python使用python语言,比较两个字符串是否相同的实例

    使用python语言,比较两个字符串是否相同的实例

    今天小编就为大家分享一篇使用python语言,比较两个字符串是否相同的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    A_thermal13542021-03-11
  • Python聊聊python里如何用Borg pattern实现的单例模式

    聊聊python里如何用Borg pattern实现的单例模式

    这篇文章主要介绍了聊聊python里如何用Borg pattern实现的单例模式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    kid_learning7792021-07-02