我想应用多处理模块来加快numpy数组元素的更新。
这是一个在制品代码...
counter = Value(c_int) # defaults to 0
# counter_lock = Lock()
def increment():
with counter.get_lock():
counter.value += 1
print(f'\r{counter.value}/{total} has been completed.',end='')
total = len(data_paths)
# Assign number to the data
data_w_num=list(enumerate(data_paths))
def prepare_data(data_w_num):
i,imgs = data_w_num
try:
with data.get_lock():
data[i] = np.array([preprocess(read_img(imgs[m]),input_shape[1:]) for m in ['t1','t2','t1ce','flair']],dtype=np.float32)
with data.get_lock():
labels[i] = preprocess_label(read_img(imgs['seg']),input_shape[1:])[None,...]
# Print the progress bar
increment()
except Exception as e:
print(f'Something went wrong with {imgs["t1"]},skipping...\n Exception:\n{str(e)}')
return
with Pool(cpu_count()) as p:
print('total: ' + f'{total}')
r = p.map_async(prepare_data,data_w_num)
print("Processing...")
r.wait()
print('\nAll processes were done.')
data_paths变量如下:
但是它不起作用。 错误消息是“只能将size-1数组转换为Python标量。” 而且我不确定如何应用多重处理。
如果您能帮助我解决这个问题,将非常高兴。