pytorch?plt.savefig()的用法及保存路徑
圖像有時(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],再?gòu)腃HW轉(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],再?gòu)腃HW轉(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é)
到此這篇關(guān)于pytorch plt.savefig()用法及保存路徑的文章就介紹到這了,更多相關(guān)plt.savefig()用法及路徑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一條命令解決mac版本python IDLE不能輸入中文問題
本文通過一條命令幫助大家解決mac版本python IDLE無法輸入中文問題,需要的朋友可以參考下2018-05-05
Python爬蟲實(shí)戰(zhàn)之使用Scrapy爬取豆瓣圖片
在用Python的urllib和BeautifulSoup寫過了很多爬蟲之后,本人決定嘗試著名的Python爬蟲框架——Scrapy.本次分享將詳細(xì)講述如何利用Scrapy來下載豆瓣名人圖片,需要的朋友可以參考下2021-06-06
Pytorch卷積神經(jīng)網(wǎng)絡(luò)遷移學(xué)習(xí)的目標(biāo)及好處
這篇文章主要為大家介紹了Pytorch卷積神經(jīng)網(wǎng)絡(luò)遷移學(xué)習(xí)的目標(biāo)實(shí)現(xiàn)代碼及好處介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
用Python復(fù)現(xiàn)二戰(zhàn)德軍enigma密碼機(jī)
大家好,本篇文章主要講的是用Python復(fù)現(xiàn)二戰(zhàn)德軍enigma密碼機(jī),感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01
對(duì)python mayavi三維繪圖的實(shí)現(xiàn)詳解
今天小編就為大家分享一篇對(duì)python mayavi三維繪圖的實(shí)現(xiàn)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Flask之請(qǐng)求鉤子的實(shí)現(xiàn)
這篇文章主要介紹了Flask之請(qǐng)求鉤子的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Python使用Asyncio進(jìn)行web編程方法詳解
這篇文章主要為大家介紹了Python使用Asyncio進(jìn)行web編程的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Python Serial串口基本操作(收發(fā)數(shù)據(jù))
這篇文章主要介紹了Python Serial串口基本操作(收發(fā)數(shù)據(jù)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
新手學(xué)python應(yīng)該下哪個(gè)版本
在本篇內(nèi)容中小編給大家整理的是關(guān)于新手學(xué)python應(yīng)該下版本的相關(guān)知識(shí)點(diǎn),需要的朋友們可以參考學(xué)習(xí)下。2020-06-06

