python2和python3的异同
python3
import importlib importlib.reload(sys)
- pyhton3中的print需要加括号,python2不需要
```python
print "Epoch {0} complete".format(j)
print ("Epoch {0} complete".format(j))# python3
- 在python2里,range直接生成list,而xrange生成的是生成器。而在python3里,两者合并成range,生成一个生成器。python3中将文件里有用到xrange的地方改为range。
- zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中为了减少内存,zip() 返回的是一个对象。如需展示列表,需手动 list() 转换。
安装包
```python pip install torch==1.4.0 -f https://download.pytorch.org/whl/torch_stable.html
<a name="aLypo"></a>
# 控制台指令
<a name="A6eCZ"></a>
## conda
- 介绍:一个开源的软件包管理系统和环境管理系统
| 指令 | 功能 | 备注 |
| --- | --- | --- |
| **conda create --name graPro** | 新建一个python环境 | |
| **conda env remove -n graPro** | 删除一个名(-n是name的意思)为graPro的环境 | |
| **conda activate graPro** | 激活名为graPro的python环境 | |
| **conda deactivate** | 退出当前环境至默认的base环境 | |
| **conda env list** | 查看conda环境列表 | |
| | | |
<a name="j7QLp"></a>
## pip
- 介绍:python的包管理工具
| 指令 | 功能 | 备注 |
| --- | --- | --- |
| **pip install torch==1.4.0** | 下载torch的1.4.0版本号 | |
| | <br />- -f<br /> - 官方解释:_-f, --find-links <url>_<br /> - 功能:从指定url文件中查找包的下载链接,而不是从默认的pip安装源。<br /> | |
| **conda activate graPro** | 激活名为graPro的python环境 | |
| **conda deactivate** | 退出当前环境至默认的base环境 | |
| **conda env list** | 查看conda环境列表 | |
<a name="bOxT4"></a>
# 函数
<a name="xDACx"></a>
## 内置函数
- __getitem__()
- 如果类把某个属性定义为序列,可以使用__getitem__()输出序列属性中的某个元素
<a name="tRe8k"></a>
# Numpy
<a name="Bw5LW"></a>
## 函数
- np.arange(3, 9)
- 产生一个起始点为3,终止点为8,默认步长为1的等差序列
- 第三个可选参数可以设置步长
- 若只有一个参数,如np.arange(3),则默认起始点为0,步长为1,产生三个数
- np.hsplit(arr, 3) / np.hsplit(arr, (2, 4, 5))
- 纵轴,切割
- 按纵轴切割,切成3个等shape的数组
- 按纵轴切割,分别在2前面(从0开始),4前面,5前面划一刀
- np.hstack((arr1, arr2)) <==>np.c_[arr1, arr2]
- 纵轴,堆叠
```python
a:[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
b:[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
c:[[ 1 2 3 4 1 2 3 4]
[ 5 6 7 8 5 6 7 8]
[ 9 10 11 12 9 10 11 12]]
- numpy.linspace(0, 1, 50)
- 产生一个起始值为0,终止值为1,共包含50个数的等差序列
- numpy.meshgrid(arr1, arr2)
- 从坐标向量中返回坐标矩阵
- 第一个参数是x坐标,第二个参数是y坐标。返回值有两个,第一个是配对好的x坐标,第二个是配对好的y坐标。
- https://blog.csdn.net/qq_38701868/article/details/99694048
- numpy.random.normal(loc=0,scale=1e-2,size=shape)
- 正态分布
- 参数loc(float):正态分布的均值,对应着这个分布的中心。loc=0说明这一个以Y轴为对称轴的正态分布。
- 参数scale(float):正态分布的标准差,对应分布的宽度,scale越大,正态分布的曲线越矮胖,scale越小,曲线越高瘦。
- 参数size(int 或者整数元组):输出的值赋在shape里,默认为None。
- 正态分布
- np.random.rand(100, 50)
- 生成100行,50列的浮点数,服从“0~1”均匀分布的随机样本值,[0,1)
- numpy.random.randint(low, high=None, size=None, dtype=’l’)
- 生成随机整数,范围[low, high)
- high没有填写时,默认生成随机数的范围是[0,low)
- dtype—为数据类型,默认的数据类型是np.int
- np.random.randn(100, 50)
- 生成服从标准正态分布(均值为0,标准差为1)的随机样本值
- np.random.random((100, 50)) / np.random.random([100, 50])
- 生成100行,50列的浮点数,数值在0~1之间。
np.random.seed(1)
- 设定相同的种子,可以生成相同的随机数
np.random.seed(1)
L1 = np.random.randn(3, 3)
np.random.seed(1) #必须再调用一次
L2 = np.random.randn(3, 3)
print(L1)
print(L2)
- 设定相同的种子,可以生成相同的随机数
np.random.uniform(1, 2)
- 在界定范围内随机取一个值
- np.resize(arr, (2, 4))
- resize直接改变原数组的结构
- reshape不改变原数组的结构,使用方法也不同
- arr2 = arr1.reshape((2, 4))
- np.stack((arr1, arr2), axis = 0)
- 第一个参数里的数组必须形状相同
- 输出数组在axis指定的维度上增加一个维度,该维度的长度是第一个参数里数组的个数
- np.vsplit(arr, 3) / np.vsplit(arr, (2, 4, 5))
- 横轴,切割
- 按横轴切割,切成3个等shape的数组
- 按横轴切割,分别在2索引前面,4索引前面,5索引前面划一刀
- 横轴,切割
- np.vstack(arr1, arr2)<==> np.r_[arr1, arr2]
- 横轴,堆叠 ```python a:[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
b:[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
c:[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
<a name="wD3Gx"></a>
## 语法
- numpy浅拷贝
- numpy挨个改变值的简写
```python
b = [1, 2 ,3]
b = np.array(b)
a = b
a[a < 2] = 0
print(a)# [0 2 3]
print(b)# [0 2 3]
# 注意a、b都改变了
数组维度
a=[1,2,3]
b=[4,5,6]
c = np.vstack((a,b))
print(c)
# [[1 2 3]
# [4 5 6]]
print(c[1])
# [4 5 6]
print(c[1, 2])
# 6
print(c[..., -1]) # 所有最后一个维度的最后一个值
# [3 6]
数组里的None
- 会在指定位置,增加一个长度为1的维度
d = np.array([1, 1])
print(d.shape) # (2,)
f = d[None, :]
print(f.shape) # (1, 2)
# -----------------------------
a = np.random.rand(1,25, 3)
b = np.random.rand(25, 4)
c = np.repeat(a[:, :, -1:], 3, 0)
d = np.repeat(b[None, :, -1:], 3, 0)
print(c.shape)# (3, 25, 1)
print(d.shape)# (3, 25, 1)
cv2
函数
- 会在指定位置,增加一个长度为1的维度
cv2.imwrite()
totalFrames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
计算总帧数
for cnt in tqdm(range(totalFrames), desc=’{:10s}’.format(os.path.basename(videoname))):
tqdm是进度条,‘:10s’代表的是视频名和进度条的间隔,冒号不能去掉
ret, frame = video.read()
# ret,frame将会获取cap.read()方法的两个返回值。
# ret:布尔值,若读取帧未到结尾返回True,反之返回False。
# frame:通过三维矩阵存储的每一帧的信息。
if cnt < start:
continue
if cnt >= end:
break
if not ret:
continue
# 提取视频中的帧,并将其保存为图片,注意路径中不能有中文
cv2.imwrite(join(outpath, '{:06d}.jpg'.format(cnt)), frame)
关闭这个流
video.release()
<a name="G4YgW"></a>
# OS
<a name="JRT2P"></a>
## 函数
- os.walk("./video")
- 功能:遍历文件夹
```python
for root, dirs, files in os.walk("./video"):
for file in files:
print(file)
src = os.path.join(root, file)
print(src)
# 理解:
# 1 如果目标文件夹下没有子文件夹,则大循环只会做一次,root、dirs、files分别是三个元组
# 2 files是元组,file是字符串
# 3 大循环所做的次数应该是目标文件夹和其子文件夹下文件数的总和,详见:https://blog.csdn.net/qq_37344125/article/details/107972463
shutile
函数
- shutile.copyfile(srcfile, drcfile)
- 功能:复制粘贴文件