python
bigfly Lv4

this page to show python code modules



matplotlab绘制损失&精度图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

# plt 读取权重文件绘制图形
# 权重文件.txt 形式如下
# {"train_lr": 7.806273333612983e-07, "train_min_lr": 7.806273333612983e-07, "train_loss": 6.33559472168852, "train_loss_scale": 8240.107382550335, "train_weight_decay": 0.049999999999998955, "train_grad_norm": Infinity, "val_loss": 4.888336278969729, "val_acc1": 1.480306729733424, "val_acc5": 7.903780453929251, "epoch": 0, "n_parameters": 133832650}

# 读代码如下 jupyter 读取文件并绘制图形代码如下:

%matplotlib inline
import matplotlib.pyplot as plt
import json
import numpy as np

file = open('data.txt') # 打开文档
data = file.readlines() # 读取文档数据

def pluck(lst, key):
return [x.get(key) for x in lst]

d = []
for i in data : #str to dict
c = json.loads(i)
d.append(c)

print(type(data[0]))
print(type(d[0]))


fig = plt.figure(figsize = (7,5)) #figsize是图片的大小
ax1 = fig.add_subplot(1, 1, 1) # ax1是子图的名字
# “-”代表画的曲线是实线,可自行选择,label代表的是图例的名称,一般要在名称前面加一个u,如果名称是中文,会显示不出来,目前还不知道怎么解决。
train_loss_line = plt.plot(pluck(d, 'epoch'),pluck(d, 'train_loss'),'r-', label = u'train_loss')
#显示图例
val_loss_line = plt.plot(pluck(d, 'epoch'),pluck(d, 'val_loss'), 'b-', label = u'val_loss')
plt.legend()
plt.xlabel(u'epoch')
plt.ylabel(u'loss')
plt.title('Compare loss for different epoch in training')
plt.show()

fig = plt.figure(figsize = (7,5)) #figsize是图片的大小
ax2 = fig.add_subplot(1, 1, 1) # ax2是子图的名字
p2 = plt.plot(pluck(d, 'epoch'),pluck(d, 'val_acc1'),'r-', label = u'Top-1')
p3 = plt.plot(pluck(d, 'epoch'),pluck(d, 'val_acc5'), 'b-', label = u'Top-5')
plt.legend()
plt.xlabel(u'epoch')
plt.ylabel(u'accuracy')
plt.title('Compare acc of Top-1 and Top-5')
plt.show()

批量视频数据提取帧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

import os.path
import cv2


# 参数:
# path: 要操作文件的根目录
# list: 默认给 [] 即可
# 功能:
# 将path路径下的所有文件夹名称存入列表,并返回
def traversalDir_FirstDir(path, list):
if (os.path.exists(path)):
files = os.listdir(path)
for file in files:
m = os.path.join(path, file)
if (os.path.isdir(m)):
h = os.path.split(m)
list.append(h[1])

return list


# 一个测试函数
def test_traversalDir_FirstDir():
dir = "C:\\Users\\Bigfly\\Desktop\\frame\\test"
list = traversalDir_FirstDir(dir, [])

print(list)


# 参数:
# timerate: 视频截帧间隔
# rootdir: 想要操作文件的根目录
# video_subname: 视频文件的后缀名 例如 ‘.mp4’
# 函数功能:
# 通过时间间隔截取视频帧
def video2frame(timerate, rootdir, video_subname):
n = 0
path = os.path.dirname(rootdir) # 拿到视频所在文件夹的上一层文件夹
# print(path)
# if not os.path.join(path,"save_frame"):
os.mkdir(os.path.join(path, "save_frame"))
save_frame_rootdir = os.path.join(path, "save_frame") # 创建一个保存帧的文件夹
# print(save_frame_rootdir)
dirlist = traversalDir_FirstDir(rootdir, []) # 得到视频所在文件夹下,eg 第n组

for i in range(0, len(dirlist)):

# if not os.path.join(save_frame_rootdir,dirlist[i]):
os.mkdir(os.path.join(save_frame_rootdir, dirlist[i]))
save_frame_subdir_i = os.path.join(save_frame_rootdir, dirlist[i]) # 保存帧的子文件夹 eg 第n组

for root, dirs, names in os.walk(os.path.join(rootdir, dirlist[i])): # 开始在视频文件夹处理视频
for name in names:
pre_name = os.path.splitext(name)[0]
ext = os.path.splitext(name)[1]
# if not os.path.join(save_frame_subdir_i, pre_name):
os.mkdir(os.path.join(save_frame_subdir_i, pre_name))
save_frame_dir = os.path.join(save_frame_subdir_i, pre_name)
# print(save_frame_dir)
if ext == video_subname:
# print(ext)
fromdir = os.path.join(root, name) # .mp4的绝对路径
# print(fromdir)
cap = cv2.VideoCapture(fromdir)
c = 1
while (True):
ret, frame = cap.read()
FPS = cap.get(5)
if ret:
frameRate = int(FPS) * timerate
# print(frameRate)
if (c % frameRate == 0):
# print(frameRate)
# cv2.imshow(frame)
st = save_frame_dir + str("\\") + str(n).zfill(6) + '.jpg'
# print(st)
cv2.imwrite(st, frame)

n += 1
c += 1
cv2.waitKey(0)
else:
print(dirlist[i] + " " + pre_name + "照度处理完毕")
break
cap.release()


if __name__ == '__main__':


video2frame(2, "C:\\Users\\Bigfly\\Desktop\\frame\\test", '.mp4')