日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

pytorch?plt.savefig()的用法及保存路徑_python

作者:因?yàn)楹唵危钥鞓?? 更新時(shí)間: 2022-04-08 編程語言

圖像有時(shí)候比數(shù)據(jù)更能滿足人們的視覺需求

Pytorch中保存圖片的方式

pytorch下保存圖像有很多種方法,但是這些基本上都是基于圖像處理的,將圖像的像素指定一定的維度 ,方法如下:

1、tensor直接保存

#!/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)
    # 復(fù)制一份
    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轉(zhuǎn)cv2保存

#!/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)
    # 復(fù)制一份
    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]轉(zhuǎn)化為[0,255],再從CHW轉(zhuǎn)為HWC,最后轉(zhuǎn)為cv2
    input_tensor = input_tensor.mul_(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).type(torch.uint8).numpy()
    # RGB轉(zhuǎn)BRG
    input_tensor = cv2.cvtColor(input_tensor, cv2.COLOR_RGB2BGR)
    cv2.imwrite(filename, input_tensor)

3、tensor轉(zhuǎn)pillow保存

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)
    # 復(fù)制一份
    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]轉(zhuǎn)化為[0,255],再從CHW轉(zhuǎn)為HWC,最后轉(zhuǎn)為numpy
    input_tensor = input_tensor.mul_(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).type(torch.uint8).numpy()
    # 轉(zhuǎn)成pillow
    im = Image.fromarray(input_tensor)
    im.save(filename)

?主要是寫一些函數(shù)來保存圖片;

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

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

此語句同樣需要轉(zhuǎn)化像素。

那么如果

我只需要打開一個(gè)視窗,觀察訓(xùn)練過程中圖像的變化,我對(duì)圖像像素保存沒有什么需求,只是保存一個(gè)視窗,那么我需要的保存圖像的函數(shù)僅僅是一個(gè)

plt.savefig

plt.savefig的用法以及保存的路徑,及訓(xùn)練過程中不會(huì)被覆蓋掉,可以上代碼供大家參考

        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)

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

?已經(jīng)都整整齊齊的在我的保存路徑下了。

總結(jié)

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

欄目分類
最近更新