提示:CV代码细节总结一
看代码的总结
示例:
(1)skimage.io.imread
H×W×C,RGB,数据类型 numpy.ndarray
(2)PIL.Image.open
H×W×C,RGB,数据类型 PIL.JpegImagePlugin.JpegImageFile
通过 numpy.asarray(image_pil) 可转成 numpy.ndarray
(3)cv2.imread
H×W×C,BGR,数据类型 numpy.ndarray
通过 cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB) 可转成 RGB
代码如下(示例):
是以图像左上角为原点
x方向为从左往右
y方向为从上往下
|————————> x
|
|
V
y
求范数方法:
x_norm=np.linalg.norm(x, ord=None, axis=None, keepdims=False)
① x: 表示矩阵(也可以是一维)
② ord:范数类型
默认二范数: ( x 2 ) + ( y 2 ) \sqrt{(x^2) + (y^2)} (x2)+(y2)
# Numpy.sqrt() 函数计算给定数组中每个元素的平方根。
# numpy.square() 平方
arctan 就是反正切的意思,例如:tan45度=1,则
arttan1=45度,就是求“逆”的运算,就好比乘法的“逆”运算是除法一样。
类似的还有arcsin就是反正弦
sin30度=1/2,则arcsin1/2=30度
此外,还有arccos 和arccot 等等……
在Python中,切片(slice)是对序列型对象(如list
, string
, tuple
)的一种高级索引方法。普通索引只取出序列中一个下标对应的元素,而切片取出序列中一个范围对应的元素,这里的范围不是狭义上的连续片段。下面的代码初步展示了切片索引的力量。
>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:5]
[0, 1, 2, 3, 4]
>>> a[5:]
[5, 6, 7, 8, 9]
>>> a[2:8] # 左包含 右不包含
[2, 3, 4, 5, 6, 7]
>>> a[::2]
[0, 2, 4, 6, 8]
>>> a[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
** cat()**
函数目的: 在给定维度上对输入的张量序列seq 进行连接操作。
outputs = torch.cat(inputs, dim=0) → Tensor
参数
inputs : 待连接的张量序列,可以是任意相同Tensor
类型的python 序列
dim : 选择的扩维, 必须在0
到len(inputs[0])
之间,沿着此维连接张量序列。
重点
输入数据必须是序列,序列中数据是任意相同的shape
的同类型tensor
维度不可以超过输入数据的任一个张量的维度
import torch
# x1
x1 = torch.tensor([[11,21,31],[21,31,41]],dtype=torch.int)
x1.shape # torch.Size([2, 3])
# x2
x2 = torch.tensor([[12,22,32],[22,32,42]],dtype=torch.int)
x2.shape # torch.Size([2, 3])
'inputs为2个形状为[2 , 3]的矩阵 '
inputs = [x1, x2]
print(inputs)
print('*' * 30)
print(len(inputs))
'''
[tensor([[11, 21, 31],
[21, 31, 41]], dtype=torch.int32), tensor([[12, 22, 32],
[22, 32, 42]], dtype=torch.int32)]
******************************
2
'''
torch.cat(inputs, dim=0).shape # torch.Size([4, 3])
torch.cat(inputs, dim=1).shape # torch.Size([2, 6])
torch.unsqueeze(input, dim, out=None)
返回一个新的张量,对输入的既定位置插入维度 1
如果dim为负,则将会被转化dim+input.dim()+1
tensor (Tensor)
– 输入张量dim (int)
– 插入维度的索引out (Tensor, optional)
– 结果张量torch.sign(input, out=None)
说明:符号函数,返回一个新张量,包含输入input张量每个元素的正负(大于0的元素对应1,小于0的元素对应-1,0还是0)
参数:
a = torch.randn(4)
a
tensor([ 0.7734, 0.5677, -0.3896, 1.9878])
torch.sign(a)
tensor([ 1., 1., -1., 1.])
pip install torchsummary
from torchsummary import summary
summary(your_model, input_size=(channels, H, W))