Python:敲代码做笔记
2021/4/24 14:26:54
本文主要是介绍Python:敲代码做笔记,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1. np.transpose()
函数原型:
transpose(a, axes=None)
参数:
a
:输入数组axes
:可选的一组list
,根据给定的list
调换数组各位置的值(我也不知道怎么表述,直接看下面的例子吧),默认将数组各维度反转(矩阵转置)
返回值:ndarray
类型,变换后的数组视图
示例1:一维数组
import numpy as np t = np.arange(4) print(t) print(t.transpose())
输出:
[0, 1, 2, 3] [0, 1, 2, 3]
可以看出这个方法对一维数组不管用
示例2:二维数组
two = np.arange(16).reshape(4, 4) print(two) print(two.transpose())
输出:
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] [[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 11], [3, 7, 11, 15]]
对二维数组的操作默认就是矩阵转置;
示例3:三维数组
three = np.arange(18).reshape(2, 3, 3) print(three) print(three.transpose())
输出:
[[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]]] [[[0, 9], [3, 12], [6, 15]], [[1, 10], [4, 13], [7, 16]]
详解:
数组three
的原始axes
为(0, 1, 2)
,那么transpose()
默认的参数axes
为:(2, 1, 0)
先看原始数组各值对应的索引:
three[0][0][0]=0; three[0][0][1]=1; three[0][0][2]=2; three[0][1][0]=3; three[0][1][1]=4; three[0][1][2]=5; three[0][2][0]=6; three[0][2][1]=7; three[0][2][2]=8; three[1][0][0]=9; three[1][0][1]=10; three[1][0][2]=11; three[1][1][0]=12; three[1][1][1]=13; three[1][1][2]=14; three[1][2][0]=15; three[1][2][1]=16; three[1][2][2]=17;
再看经过transpose()
后的数组x
,主要操作就是将各个元素的对应下标换一下
three[0][0][0]=0; three[1][0][0]=1; three[2][0][0]=2; three[0][1][0]=3; three[1][1][0]=4; three[2][1][0]=5; three[0][2][0]=6; three[1][2][0]=7; three[2][2][0]=8; three[0][0][1]=9; three[1][0][1]=10; three[2][0][1]=11; three[0][1][1]=12; three[1][1][1]=13; three[2][1][1]=14; three[0][2][1]=15; three[1][2][1]=16; three[2][2][1]=17;
看一下对图像的操作:
import cv2 as cv import numpy as np img = cv.imread("C:\\Users\\mac\\Pictures\\0.jpg") cv.imshow('img', img) img = np.transpose(img, (1, 0, 2)) cv.namedWindow('img', cv.WINDOW_AUTOSIZE) cv.imshow('img2', img) cv.waitKey(0) cv.destroyAllWindows()
原始图像:
变换后的图像:
参考链接:
- np.transpose的作用及在图像操作中的使用
- np.transpose
2. torchvision.utils.make_grid()
用于把几个图像按照网格排列的方式绘制出来。
函数原型:
make_grid( tensor: Union[torch.Tensor, List[torch.Tensor]], nrow: int = 8, padding: int = 2, normalize: bool = False, range: Optional[Tuple[int, int]] = None, scale_each: bool = False, pad_value: int = 0, )
参数:
tensor
:4D
张量,形状为(BxCxHxW)
,分别表示样本数,通道数,图像高度,图像宽度。或者是一个图像列表nrow
:每行的图片数量,默认为8padding
:相邻图像之间的间隔,默认为2normalize
:如果为True
,则把图像的像素值通过range
指定的最大值和最小值归一化到0-1
;默认为False
scale_each
:如果为True
,就单独对每张图像进行normalize
;如果是False
,统一对所有图像进行normalize
;默认为False
pad_value
:float
,图像之间的空隙,默认为0
示例1
from torchvision.utils import make_grid import numpy as np import torch import matplotlib.pyplot as plt def show(img): npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0)), interpolation='nearest') images = torch.FloatTensor(100 * np.random.normal(0, 1, (25, 1, 28, 28))) show(make_grid(images, nrow=5, padding=10, pad_value=0)) plt.show()
显示结果:
### 参考链接:
- Pytorch save_image和make_grid函数详解
这篇关于Python:敲代码做笔记的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python