Skip to content

Commit b9089ff

Browse files
committed
fixed minor bugs when using different numbers of processes
- mg_motion_mp will now produce _exactly_ the same results with any number of processes (tested from 1 to 12). - added num_processes parameter #213
1 parent 32c4dee commit b9089ff

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

musicalgestures/_motionvideo_mp_render.py

+8-23
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def mg_motion_mp(args):
125125
motion_frame_rgb = motion_frame
126126

127127
if save_video:
128-
# if this is not the first process (rendering the start of the video) then don't save the first frame of the output (it'll always be black).
128+
# if this is not the first process (rendering the start of the video) then drop the first frame of the output (it'll always be black).
129129
if process_id != 0 and ii > 0:
130130
if inverted_motionvideo:
131131
out.write(cv2.bitwise_not(
@@ -185,9 +185,7 @@ def mg_motion_mp(args):
185185

186186
def run_pool(func, args, numprocesses):
187187
pool = multiprocessing.Pool(numprocesses)
188-
# results = pool.map(func, args)
189188
pool.map(func, args)
190-
# return results
191189

192190

193191
def calc_frame_groups(framecount, num_cores):
@@ -204,18 +202,6 @@ def calc_frame_groups(framecount, num_cores):
204202
return groups
205203

206204

207-
def testhogfunc(process_id):
208-
limit = 20
209-
count = 0
210-
while count < limit:
211-
print(process_id, count)
212-
futyi = 0
213-
for i in range(10_000_000):
214-
futyi += 1
215-
count += 1
216-
return process_id, count
217-
218-
219205
def bool_from_str(boolstring):
220206
return True if boolstring == "True" else False
221207

@@ -240,6 +226,7 @@ def bool_from_str(boolstring):
240226
parser.add_argument('save_data', metavar='save_data', type=str, help='save_data')
241227
parser.add_argument('save_motiongrams', metavar='save_motiongrams', type=str, help='save_motiongrams')
242228
parser.add_argument('save_video', metavar='save_video', type=str, help='save_video')
229+
parser.add_argument('num_processes', metavar='num_processes', type=str, help='num_processes')
243230

244231
args = parser.parse_args()
245232

@@ -257,21 +244,19 @@ def bool_from_str(boolstring):
257244
fps, width, height, length = int(float(args.fps)), int(float(args.width)), int(float(args.height)), int(float(args.length))
258245
color, filtertype, thresh, blur, kernel_size = bool_from_str(args.color), args.filtertype, float(args.thresh), args.blur, int(float(args.kernel_size))
259246
inverted_motionvideo, inverted_motiongram, equalize_motiongram = bool_from_str(args.inverted_motionvideo), bool_from_str(args.inverted_motiongram), bool_from_str(args.equalize_motiongram)
260-
save_data, save_motiongrams, save_video = bool_from_str(args.save_data), bool_from_str(args.save_motiongrams), bool_from_str(args.save_video)
247+
save_data, save_motiongrams, save_video = bool_from_str(args.save_data), bool_from_str(args.save_motiongrams), bool_from_str(args.save_video)
248+
num_processes = multiprocessing.cpu_count() if int(float(args.num_processes)) < 1 else min(int(float(args.num_processes)), multiprocessing.cpu_count())
261249

262-
numprocessors = multiprocessing.cpu_count()
263-
frame_groups = calc_frame_groups(length, numprocessors)
250+
frame_groups = calc_frame_groups(length, num_processes)
264251

265252
feed_args = []
266253

267-
for i in range(numprocessors):
254+
for i in range(num_processes):
268255
start_frame, num_frames = frame_groups[i]
269256
initargs = [target_folder, of, fex, fps, width, height, length, color, filtertype, thresh, blur, kernel_size, inverted_motionvideo, inverted_motiongram, equalize_motiongram, save_data, save_motiongrams, save_video, start_frame, num_frames, i, client]
270257
# client.sendall(bytes(str(initargs), 'utf-8'))
271258
feed_args.append(initargs)
272259

273-
# results = run_pool(mg_motion_mp, feed_args, numprocessors)
274-
run_pool(mg_motion_mp, feed_args, numprocessors)
260+
run_pool(mg_motion_mp, feed_args, num_processes)
275261

276-
client.close()
277-
# print("Closed socket client.")
262+
client.close()

musicalgestures/_motionvideo_mp_run.py

+39-12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def mg_motion_mp(
3333
target_name_data=None,
3434
target_name_mgx=None,
3535
target_name_mgy=None,
36-
overwrite=False):
36+
overwrite=False,
37+
num_processes=-1):
3738

3839
of, fex = self.of, self.fex
3940

@@ -64,7 +65,7 @@ def mg_motion_mp(
6465

6566
save_data_feed = save_data or save_plot
6667

67-
command = [pythonkw, pyfile, temp_folder, of_feed, fex, self.fps, self.width, self.height, self.length, self.color, filtertype, thresh, blur, kernel_size, inverted_motionvideo, inverted_motiongram, equalize_motiongram, save_data_feed, save_motiongrams, save_video]
68+
command = [pythonkw, pyfile, temp_folder, of_feed, fex, self.fps, self.width, self.height, self.length, self.color, filtertype, thresh, blur, kernel_size, inverted_motionvideo, inverted_motiongram, equalize_motiongram, save_data_feed, save_motiongrams, save_video, num_processes]
6869
command = [str(item) for item in command]
6970
# print()
7071
# print(command)
@@ -108,28 +109,54 @@ def mg_motion_mp(
108109
# print("organizing results...")
109110
results = os.listdir(temp_folder)
110111
time_files = [temp_folder + file for file in results if file.startswith("time")]
112+
time_files.sort()
111113
com_files = [temp_folder + file for file in results if file.startswith("com")]
114+
com_files.sort()
112115
qom_files = [temp_folder + file for file in results if file.startswith("qom")]
116+
qom_files.sort()
113117
gramx_files = [temp_folder + file for file in results if file.startswith("gramx")]
118+
gramx_files.sort()
114119
gramy_files = [temp_folder + file for file in results if file.startswith("gramy")]
120+
gramy_files.sort()
115121
video_files = [temp_folder + file for file in results if file.endswith("avi")]
122+
video_files.sort()
116123

117124
gramx, gramy, time, com, qom = None, None, None, None, None
118125

119126
if save_motiongrams:
120127
# load gramx
121-
for idx, item in enumerate(gramx_files):
122-
if idx == 0:
123-
gramx = np.load(item)
124-
else:
125-
gramx = np.append(gramx, np.load(item)[1:-1], axis=0)
128+
# if we only used a single chunk, load everything
129+
if len(gramx_files) == 1:
130+
gramx = np.load(gramx_files[0])
131+
# or in case there were multiple chunks...
132+
else:
133+
for idx, item in enumerate(gramx_files):
134+
if idx == 0:
135+
# do not drop first row in first chunk
136+
gramx = np.load(item)[:-1]
137+
elif idx == len(gramy_files) - 1:
138+
# do not drop the last row in last chunk
139+
gramx = np.append(gramx, np.load(item)[1:], axis=0)
140+
else:
141+
# else drop first and last rows from chunk
142+
gramx = np.append(gramx, np.load(item)[1:-1], axis=0)
126143

127144
# load gramy
128-
for idx, item in enumerate(gramy_files):
129-
if idx == 0:
130-
gramy = np.load(item)
131-
else:
132-
gramy = np.append(gramy, np.load(item)[:, 1:], axis=1)
145+
# if we only used a single chunk, load everything
146+
if len(gramy_files) == 1:
147+
gramy = np.load(gramy_files[0])
148+
# or in case there were multiple chunks...
149+
else:
150+
for idx, item in enumerate(gramy_files):
151+
if idx == 0:
152+
# do not drop first column in first chunk
153+
gramy = np.load(item)[:, :-1]
154+
elif idx == len(gramy_files) - 1:
155+
# do not drop the last column in last chunk
156+
gramy = np.append(gramy, np.load(item)[:, 1:], axis=1)
157+
else:
158+
# else drop first and last columns from chunk
159+
gramy = np.append(gramy, np.load(item)[:, 1:-1], axis=1)
133160

134161
if self.color == False:
135162
# Normalize before converting to uint8 to keep precision

0 commit comments

Comments
 (0)