-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_parallel_multiprocessing.py
More file actions
66 lines (51 loc) · 1.79 KB
/
convert_parallel_multiprocessing.py
File metadata and controls
66 lines (51 loc) · 1.79 KB
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
#!/usr/bin/python
# py2: 46sec py3: 50sec
#from __future__ import print_function
import multiprocessing
import os
import ctypes
import array as ar
import numpy as np
import sys
from scipy.misc import imread
from scipy.linalg import svd
import time
t1=time.time()
dirname = "/nvme/bm/img/"
files = [filename for (dirpath, dirnames, filenames) in os.walk(dirname)
for filename in filenames if filename[-4:] == ".jpg"]
files=sorted(files)
if len(files) == 0:
print('ERROR goto folder im_align_celeba and inspect the README file\n')
sys.exit(1)
def worker(num,num2,workerData):
print( 'spawning: '+str(num)+' '+str(num2)+'\n')
for i in range(num,num2+1):
workerData[i]=np.mean(imread(dirname+files[i])[::4,::4,:],axis=2).reshape(-1)
return
if __name__ == '__main__':
shared_array_base = multiprocessing.Array(ctypes.c_float, 202599*55*45, lock=False)
data = np.frombuffer(shared_array_base, dtype=ctypes.c_float)
data = data.reshape(202599,55*45)
print('data.shape: ' +str(data.shape) + '\n')
nr_threads=8
imgcount=len(files)
batches=imgcount//nr_threads
processes = []
idxstart=0
idxstop=batches-1
print( 'images: ' + str(imgcount) + ' in batches of size: ' + str(batches) + ' with ' + str(nr_threads) + ' threads')
for i in range(nr_threads):
p = multiprocessing.Process(target=worker, args=[idxstart,idxstop,data])
processes.append(p)
p.start()
idxstart+=batches
idxstop+=batches
for i in processes:
i.join()
if(batches*nr_threads < imgcount):
worker(batches*nr_threads,imgcount-1,data)
with open("/nvme/bm/convert_multiprocessing", "wb") as f:
np.asarray(data, dtype=np.float32).tofile(f)
t2=time.time()-t1
print(str(t2) + ' seconds')