forked from DeepLabCut/DeepLabCut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testscript_pretrained_models.py
174 lines (157 loc) · 4.84 KB
/
testscript_pretrained_models.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Testscript human network
"""
import os, subprocess, deeplabcut
from pathlib import Path
import pandas as pd
import numpy as np
Task = "human_dancing"
YourName = "teamDLC"
MODEL_NAME = "horse_sideview" #full_human"
basepath = os.path.dirname(os.path.abspath("testscript.py"))
videoname = "reachingvideo1"
video = [
os.path.join(
basepath, "Reaching-Mackenzie-2018-08-30", "videos", videoname + ".avi"
)
]
# legacy mode:
"""
configfile, path_train_config=deeplabcut.create_pretrained_human_project(Task, YourName,video,
videotype='avi', analyzevideo=True,
createlabeledvideo=True, copy_videos=False) #must leave copy_videos=True
"""
# new way:
configfile, path_train_config = deeplabcut.create_pretrained_project(
Task,
YourName,
video,
model=MODEL_NAME,
videotype="avi",
analyzevideo=True,
createlabeledvideo=True,
copy_videos=False,
) # must leave copy_videos=True
'''
lastvalue = 5
DLC_config = deeplabcut.auxiliaryfunctions.read_plainconfig(path_train_config)
pretrainedDeeperCutweights = DLC_config["init_weights"]
print("EXTRACTING FRAMES")
deeplabcut.extract_frames(configfile, mode="automatic", userfeedback=False)
print("CREATING-SOME LABELS FOR THE FRAMES")
cfg = deeplabcut.auxiliaryfunctions.read_config(configfile)
frames = os.listdir(os.path.join(cfg["project_path"], "labeled-data", videoname))
# As this next step is manual, we update the labels by putting them on the diagonal (fixed for all frames)
for index, bodypart in enumerate(cfg["bodyparts"]):
columnindex = pd.MultiIndex.from_product(
[[cfg["scorer"]], [bodypart], ["x", "y"]],
names=["scorer", "bodyparts", "coords"],
)
frame = pd.DataFrame(
100 + np.ones((len(frames), 2)) * 50 * index,
columns=columnindex,
index=[os.path.join("labeled-data", videoname, fn) for fn in frames],
)
if index == 0:
dataFrame = frame
else:
dataFrame = pd.concat([dataFrame, frame], axis=1)
dataFrame.to_csv(
os.path.join(
cfg["project_path"],
"labeled-data",
videoname,
"CollectedData_" + cfg["scorer"] + ".csv",
)
)
dataFrame.to_hdf(
os.path.join(
cfg["project_path"],
"labeled-data",
videoname,
"CollectedData_" + cfg["scorer"] + ".h5",
),
"df_with_missing",
format="table",
mode="w",
)
deeplabcut.create_training_dataset(configfile, Shuffles=[1])
edits = {
"save_iters": lastvalue,
"display_iters": 1,
"multi_step": [[0.001, lastvalue]],
"init_weights": pretrainedDeeperCutweights.split(".index")[0],
}
DLC_config = deeplabcut.auxiliaryfunctions.edit_config(path_train_config, edits)
deeplabcut.train_network(configfile, shuffle=1)
print("Adding bodypart!")
cfg = deeplabcut.auxiliaryfunctions.read_config(configfile)
cfg["bodyparts"] = [
"ankle1",
"knee1",
"hip1",
"hip2",
"knee2",
"ankle2",
"wrist1",
"elbow1",
"shoulder1",
"shoulder2",
"elbow2",
"wrist2",
"chin",
"forehead",
"plus1more",
]
deeplabcut.auxiliaryfunctions.write_config(configfile, cfg)
print("CREATING-SOME More LABELS FOR THE FRAMES (including the new bodypart!)")
cfg = deeplabcut.auxiliaryfunctions.read_config(configfile)
frames = [
f
for f in os.listdir(os.path.join(cfg["project_path"], "labeled-data", videoname))
if ".png" in f
]
# As this next step is manual, we update the labels by putting them on the diagonal (fixed for all frames)
for index, bodypart in enumerate(cfg["bodyparts"]):
columnindex = pd.MultiIndex.from_product(
[[cfg["scorer"]], [bodypart], ["x", "y"]],
names=["scorer", "bodyparts", "coords"],
)
frame = pd.DataFrame(
100 + np.ones((len(frames), 2)) * 50 * index,
columns=columnindex,
index=[os.path.join("labeled-data", videoname, fn) for fn in frames],
)
if index == 0:
dataFrame = frame
else:
dataFrame = pd.concat([dataFrame, frame], axis=1)
dataFrame.to_csv(
os.path.join(
cfg["project_path"],
"labeled-data",
videoname,
"CollectedData_" + cfg["scorer"] + ".csv",
)
)
dataFrame.to_hdf(
os.path.join(
cfg["project_path"],
"labeled-data",
videoname,
"CollectedData_" + cfg["scorer"] + ".h5",
),
"df_with_missing",
format="table",
mode="w",
)
edits = {
"save_iters": lastvalue,
"display_iters": 1,
"multi_step": [[0.001, lastvalue]],
"init_weights": pretrainedDeeperCutweights.split(".index")[0],
}
DLC_config = deeplabcut.auxiliaryfunctions.edit_config(path_train_config, edits)
# deeplabcut.train_network(configfile,shuffle=1) #>> fails one body part too much!
deeplabcut.train_network(configfile, shuffle=1, keepdeconvweights=False)
'''