当前位置:网站首页>图片保存:torchvision.utils.save_image(img, imgPath)
图片保存:torchvision.utils.save_image(img, imgPath)
2022-06-23 15:09:00 【代码小白的成长】
torchvision.utils.save_image(img, imgPath)
深度学习模型中,一般使用如下方式进行图像保存(torchvision.utils中的save_image()函数),这种方式只能保存RGB彩色图像,如果网络的输出是单通道灰度图像,则该函数依然会输出三个通道,每个通道的数值都是相同的,即“伪灰度图像”,虽然从视觉效果上看不出区别,但是图像所占内存比正常情况大了两倍。
save_image()函数:
def save_image(tensor, filename, nrow=8, padding=2,
normalize=False, range=None, scale_each=False, pad_value=0):
"""Save a given Tensor into an image file. Args: tensor (Tensor or list): Image to be saved. If given a mini-batch tensor, saves the tensor as a grid of images by calling ``make_grid``. **kwargs: Other arguments are documented in ``make_grid``. """
from PIL import Image
grid = make_grid(tensor, nrow=nrow, padding=padding, pad_value=pad_value,
normalize=normalize, range=range, scale_each=scale_each)
# Add 0.5 after unnormalizing to [0, 255] to round to nearest integer
ndarr = grid.mul_(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to('cpu', torch.uint8).numpy()
im = Image.fromarray(ndarr)
im.save(filename)
解决方式
def saveImg(self, img, save_dir, type, name, Gray=False):
fname, fext = name.split('.')
imgPath = os.path.join(save_dir, "%s_%s.%s" % (fname, type, fext))
# torchvision.utils.save_image(img, imgPath)
# 改写:torchvision.utils.save_image
grid = torchvision.utils.make_grid(img, nrow=8, padding=2, pad_value=0,
normalize=False, range=None, scale_each=False)
ndarr = grid.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to('cpu', torch.uint8).numpy()
im = Image.fromarray(ndarr)
# im.show()
if Gray:
im.convert('L').save(imgPath) # Gray = 0.29900 * R + 0.58700 * G + 0.11400 * B
else:
im.save(imgPath)
保存图片的另一种方式(推荐):
def saveImg(self, img, save_dir, type, name, Gray=False):
fname, fext = name.split('.')
imgPath = os.path.join(save_dir, "%s_%s.%s" % (fname, type, fext))
img_array = self.tensor2array(img.data[0])
image_pil = Image.fromarray(img_array)
if Gray:
image_pil.convert('L').save(imgPath) # Gray = 0.29900 * R + 0.58700 * G + 0.11400 * B
else:
image_pil.save(imgPath)
# Converts a Tensor into a Numpy array
# |imtype|: the desired type of the converted numpy array
def tensor2array(self, image_tensor, imtype=np.uint8, normalize=True):
if isinstance(image_tensor, list):
image_numpy = []
for i in range(len(image_tensor)):
image_numpy.append(self.tensor2array(image_tensor[i], imtype, normalize))
return image_numpy
image_numpy = image_tensor.cpu().float().numpy()
if normalize:
image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0
else:
image_numpy = np.transpose(image_numpy, (1, 2, 0)) * 255.0
image_numpy = np.clip(image_numpy, 0, 255)
if image_numpy.shape[2] == 1 or image_numpy.shape[2] > 3:
image_numpy = image_numpy[:, :, 0]
return image_numpy.astype(imtype)
边栏推荐
猜你喜欢
Redis缓存三大异常的处理方案梳理总结

《墨者学院——SQL手工注入漏洞测试(MySQL数据库)》

【opencv450】椒盐噪声demo

Idea view View the class file idea Class folder

变压器只能转换交流电,那直流电怎么转换呢?

A transformer can only convert alternating current. How can I convert direct current?
![[普通物理] 光的衍射](/img/1a/20dbd15e0c8c91a3e59753b2f6797a.png)
[普通物理] 光的衍射

golang 重要知识:waitgroup 解析
Sorting out and summarizing the handling schemes for the three major exceptions of redis cache

Half wave loss equal thickness and equal inclination interference
随机推荐
C. Product 1 Modulo N-Codeforces Round #716 (Div. 2)
Analysis of graphical level-1 programming problem of Electronic Society: cat and mouse
Three simple tips for accelerating yarn install
RF analyzer demo setup
快速排序的简单理解
5 minutes to quickly launch web applications and APIs (vercel)
Important knowledge of golang: detailed explanation of context
详解Redis分布式锁的原理与实现
2022年个人理财利率是多少?个人如何选择理财产品?
[cloud based co creation] how manufacturing enterprises build "barcode factories"
PHP指定字段大于100正序排,小于100随机排
[opencv450] salt and pepper noise demo
现在我要买股票,怎么开户?手机开户安全么?
The idea and method of MySQL master-slave only synchronizing some libraries or tables
The meaning of FPGA abbreviations and words in engineering field
WebService interface publishing and calling
TCP协议三次握手和四次挥手抓包分析
golang 重要知识:waitgroup 解析
VIM backup history command
B. Integers Shop-Hello 2022