文章目录
常用工具
有些小方法老是忘,记一下免得去查吧
图像转换
numpy、PIL.Image转换
Img_np = np.array(Img)
Img = Image.fromarray(Img_np)
numpy、tensor转换
pytorch:
Img_np = np.array(Img_py)
Img_py = torch.from_numpy(Img_np)
loss.detach().numpy()#得到tensor的数值
tensorflow:
Img_tensor = tf.convert_to_tensor(Img_np)
session = tf.Session()
# 张量转化为ndarray
Img_np = session.run(Img_tensor)
在图像上画图
PIL
from PIL import Image,ImageDraw
img = Image.new("RGB", (800, 800), (255, 255, 255))
draw = ImageDraw.Draw(img)
draw.line((100,100,700,700),fill='red',width=5) # 画线
draw.arc([100,100,600,600],120,180,fill='blue') # 画弧线
draw.rectangle((200,200,500,500),outline = "red", fill='white', width=5) # 画矩形
draw.ellipse([300,400,400,700],outline='black',fill='white') # 画圆
draw.text([100, 500], 'aa', 'blue') # 设置 文字位置 文字内容 颜色 文字大小
img.show()
matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig = plt.figure()
ax = fig.add_subplot(111)
rect = patches.Rectangle((0.4, 0.7), 0.4, 0.15, color='r', alpha=0.5, angle=-30) # 矩形
eclipse = patches.Ellipse((0.2, 0.8), 0.2, 0.3, angle=20, color='orange', fill=True) # 椭圆
circ = plt.Circle((0.7, 0.4), 0.2, color='g', alpha=0.5) # 圆
ax.add_patch(rect)
ax.add_patch(eclipse)
ax.add_patch(circ)
plt.annotate('hello',xy=(0.3, 0.3), xycoords='data',xytext=(-30, -30),textcoords='offset points' ) # 添加注释
plt.show()
cv2
import numpy as np
import cv2
img = np.zeros((512,512,3), np.uint8)
cv2.line(img,(0,0),(511,511),(255,0,0),5) # 画线
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) # 矩形
cv2.circle(img,(447,63), 63, (0,0,255), -1) #圆
cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1) # 椭圆
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA) # 写字
cv2.imshow('line',img)
cv2.waitKey()
解析文件
解析XML
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
</data>
xml.tree:
# coding=utf-8
import xml.etree.ElementTree as ET
tree = ET.parse("xmltest.xml") #open
root = tree.getroot() #f.seek(0) <Element 'data' at 0x027EAAE0>
print(root.tag) #data 根节点
for child in root:
print(child.tag,child.attrib) #{'name': 'Liechtenstein'}
for i in child:
print(i.tag, i.text) ##获取xml: <rank updated="yes">2</rank> <year>2008</year>
解析.mat文件
import scipy.io as scio
path = '1.mat'
data = scio.loadmat(path)
# data是字典
GPU内存查看
Ubuntu
nvidia-smi # 监控GPU,
watch -n 1 nvidia-smi # 实时监控GPU,
watch -n 1 lscpu # 实时监控CPU,
ps -elf # 进程查看,
ps -elf | grep python # 查看Python子进程,
kill -9 [PID] # 杀死进程PID。
random
import random
a = random.random() # [0-1]
print(a)
b = random.randint(10,20) # int[10-20]
print(b)
c = random.randrange(2,10,3) # [2,5,8]
print(c)
d = random.uniform(1,4) # [1-4]
print(d)
e = random.choices([2,3,4,5]) # [2,3,4,5]
print(e)
f = [1,2,3,4]
random.shuffle(f) # 打乱
print(f)
g = random.sample([1,2,3,4,5],3) # 获取3个
print(g)
output:
0.5344733643328435
18
2
3.2485454291957216
[3]
[3, 4, 1, 2]
[1, 3, 2]