当前位置:网站首页>Tensor, numpy, PIL format conversion and image display
Tensor, numpy, PIL format conversion and image display
2022-07-23 14:16:00 【Dust_ Evc】
a key
cv.imshow() : Display image is BGR Format plt.imshow() : Image display yes RGB Format
Tensor : The stored data is distributed in [0, 1]Numpy : The stored data is distributed in [0, 255]
CIFAR-10 Data sets
The data set is RGB Format ;
In the use of opencv It needs to be converted into BGR Format ;
In the use of plt Display time There is no need to Transformation format ;
Example :
dict = unpickle('./dataset/cifar-10-batches-py/test_batch')
img = dict[b'data']
image = img[0]
image = image.reshape(3, 32, 32).transpose(1, 2, 0)
cv_show('image', image)
r, g, b = cv.split(image)
pic = cv.merge([b, g, r])
cv_show('pic', pic)
The picture on the left is the original , The picture on the right is a distorted picture :
format conversion
import torch
import numpy as np
# convert numpy to tensor or vise versa
np_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(np_data) # ndarray Convert to Tensor
tensor2array = torch_data.numpy() # Tensor Convert to ndarray
test = out.cpu().detach().numpy() # from CUDA Upper Tensor Convert to ndarray
Tensor ==> Numpy
import torch
import torchvision
import pickle
import cv2 as cv
transform_tensor = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
])
transform_picture = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(), # convert to Tensor Format
torchvision.transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
def cv_show(name, img):
cv.imshow(name, img)
cv.waitKey(0)
cv.destroyAllWindows()
def unpickle(file):
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
dict = unpickle('./dataset/cifar-10-batches-py/test_batch')
img = dict[b'data']
image = img[0]
image = image.reshape(3, 32, 32).transpose(1, 2, 0)
print(image)
image_Tensor = transform_tensor(image).unsqueeze(0)
print(image_Tensor) # There is no data normalization operation
image_Tensor_Nor = transform_picture(image).unsqueeze(0)
print(image_Tensor_Nor) # There is data normalization operation
[[[158 112 49]
[159 111 47]
[165 116 51]
...
[ 24 77 124]
[ 34 84 129]
[ 21 67 110]]]
tensor([[[[0.6196, 0.6235, 0.6471, ..., 0.5373, 0.4941, 0.4549],
[0.5961, 0.5922, 0.6235, ..., 0.5333, 0.4902, 0.4667],
[0.5922, 0.5922, 0.6196, ..., 0.5451, 0.5098, 0.4706],
...,
[0.6941, 0.5804, 0.5373, ..., 0.5725, 0.4235, 0.4980],
[0.6588, 0.5804, 0.5176, ..., 0.5098, 0.4941, 0.4196],
[0.6275, 0.5843, 0.5176, ..., 0.4863, 0.5059, 0.4314]]]])
tensor([[[[ 0.6338, 0.6531, 0.7694, ..., 0.2267, 0.0134, -0.1804],
[ 0.5174, 0.4981, 0.6531, ..., 0.2073, -0.0060, -0.1223],
[ 0.4981, 0.4981, 0.6338, ..., 0.2654, 0.0910, -0.1029],
...,
[ 1.2319, 0.6661, 0.4515, ..., 0.6271, -0.1143, 0.2564],
[ 1.0563, 0.6661, 0.3540, ..., 0.3149, 0.2369, -0.1338],
[ 0.9003, 0.6856, 0.3540, ..., 0.1979, 0.2954, -0.0753]]]])
Tensor turn Numpy : ( For displaying images )
mean = (0.4914, 0.4822, 0.4465)
std = (0.2023, 0.1994, 0.2010)
def tensor_numpy(image):
clean = image.clone().detach().cpu().squeeze(0) # Get rid of batch passageway (batch, C, H, W) --> (C, H, W)
clean[0] = clean[0] * std[0] + mean[0] # Data De normalization
clean[1] = clean[1] * std[1] + mean[1]
clean[2] = clean[2].mul(std[2]) + mean[2]
clean = np.around(clean.mul(255)) # Convert to color 255 [0, 1] --> [0, 255]
clean = np.uint8(clean).transpose(1, 2, 0) # Change to three channels (C, H, W) --> (H, W, C)
r, g, b = cv.split(clean) # RGB Channel switching
clean = cv.merge([b, g, r])
return clean
Num = tensor_numpy(image_Tensor_Nor)
print(Num)
If you use cv.imshow() You need to use the RGB Channel switching ;
If you use plt.imshow() There is no need to use the above RGB Channel switching ;
Example :
plt.imshow('image', image)
plt.show()
RGB Format :
[[[158 112 49]
[159 111 47]
[165 116 51]
...
[ 24 77 124]
[ 34 84 129]
[ 21 67 110]]]
RGB Channel switching :
r, g, b = cv.split(image)
pic = cv.merge([b, g, r])
plt.imshow('image', pic)
plt.show()
BGR Format :
[[[ 49 112 158]
[ 47 111 159]
[ 51 116 165]
...
[124 77 24]
[129 84 34]
[110 67 21]]]
PyTorch save_image()
torchvision.utils.save_image()
Finally, let's take a look at pytorch How does the built-in function perform format conversion and save pictures :
def save_image(
tensor: Union[torch.Tensor, List[torch.Tensor]],
fp: Union[Text, pathlib.Path, BinaryIO],
format: Optional[str] = None,
**kwargs
)
grid = make_grid(tensor, **kwargs)
# 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(fp, format=format)
边栏推荐
- Kafka consumption reports an error coordinator unavailable Rediscovery will be attempt redisCovery
- 第十一天笔记
- Day108. Shang Yitong: interface docking of hospital simulation system - query of hospital | Department | shift scheduling, addition, deletion, modification and paging conditions
- 数据库连接池 & DBUtils
- STM32输出SPWM波,HAL库,cubeMX配置,滤波后输出1KHz正弦波
- LabVIEW运行中改变Chart的历史长度
- js 实现 encode64 加密
- 天玑1100相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑1100怎么样
- 打家劫舍!
- -bash: ifconfig: command not found
猜你喜欢

天玑720相当于骁龙多少处理器 天玑720相当于骁龙多少 天玑720怎么样

581. 最短无序连续子数组

Day108. Shang Yitong: interface docking of hospital simulation system - query of hospital | Department | shift scheduling, addition, deletion, modification and paging conditions

How about the nuclear display performance of Ruilong R7 Pro 6850h? What level is it equivalent to

338. 比特位计数

Principle of container network

第五天筆記

What is Tianji 920 equivalent to a snapdragon? How much is Tianji 920 equivalent to a snapdragon? How about Tianji 920

Notes on the fifth day

数据库连接池 & DBUtils
随机推荐
采样和数据驱动
激励发生器、监测器
Network security note 1 - Security of Internet Protocol
rtx3070ti显卡什么水平 rtx3070ti显卡什么级别 rtx3070ti显卡怎么样
设计例化和连接
581. 最短无序连续子数组
LabVIEW运行中改变Chart的历史长度
拖拽----
iQOO 10 Pro和小米12 Ultra哪个好 哪个值得买 两者配置对比
Canvas eraser function
What level is rtx3090ti? What level is rtx3090ti graphics card? How about rtx3090ti graphics card
BGP联邦实验
网络安全笔记1——Internet协议的安全性
太平洋大西洋水流问题
Fabric.js 基础笔刷
笔记本酷睿i5 1135g7相当于什么水平?i5 1135g7性能怎么样
rtx3080相当于gtx什么显卡 rtx3080显卡什么水平 rtx3080显卡怎么样
Remember that a vulnhub target plane exercise successfully won the root permission
Pbootcms数据库转换教程(sqlite转mysql详细教程)
Overlayfs source code parsing

