python读取mysql数据每次都耗时600秒_Python tensorflow多进程或多线程批读取图像数据读取加速多处理池同期期货,pythontensorflow,批量,multiproces...

需求

格式为(batch, frames, h, w, c)的视频输入需要使用生成器,自定义网络的输入,但是重复batch*frames次数据的读取操作,如果默认循环读取方式,读取速度100frames/s,严重影响训练进程。

电脑CPU核心数充沛,需要使用

多进程方案,每一个batch分配一个进程,并行读取数据

multiprocessing.pool

百度搜网上基本是这段程序

import concurrent.futures

start_time1 = time.time()

def load_and_resize(image_filename):

img = cv2.imread(image_filename)

img = cv2.resize(img, (600, 600))

# create a pool of processes. By default,one is created for each cpu in your machine

with concurrent.futures.ProcessPoolExecutor() as executor:

# get a list of files to process

image_files = glob.glob('*.jpg')

# executor.map() 将你想要运行的函数和列表作为输入,列表中的每个元素都是我们函数的单个输入,由于我们有6个核,我们将同时处理该列表中的6个项目

executor.map(load_and_resize, image_files)

print('acceleration time:', time.time() - start_time1)

但十分bug的一点:

注意:

在Windows上要想使用进程模块,就必须把有关进程的代码写在当前.py文件的if __name __== ‘__main __’ :语句的下面

,才能正常使用Windows下的进程模块,Unix/Linux下则不需要。

https://blog.csdn.net/Alvin_FZW/article/details/82886004

Python官方的解释参考:

避免共享状态、使用 Join 避免僵尸进程、避免杀死进程

https://docs.python.org/zh-cn/3/library/multiprocessing.html

https://docs.python.org/zh-cn/3/library/multiprocessing.html#multiprocessing-programming

因此无法适用于我的需求,放弃此方案。

concurrent.futures.

concurrent.futures.ProcessPoolExecutor

百度搜网上基本是这段程序

import concurrent.futures

def load_and_resize(image_filename):

img = cv2.imread(image_filename)

# img = cv2.resize(img, (600, 600))

start_time1 = time.time()

with concurrent.futures.ProcessPoolExecutor() as executor: ## 默认为1

image_files = glob.glob(image_path + '\\*.jpg')

'''

executor.map() 将你想要运行的函数和列表作为输入,列表中的每个元素

都是我们函数的单个输入,由于我们有6个核,我们将同时处理该列表中的6个项目

'''

executor.map(load_and_resize, image_files)

print('多核并行加速后运行 time:', round(time.time() - start_time1, 2), " 秒")

但是同multiprocessing.pool相同需要放在if __name __== ‘__main __’ :语句的下面

The

main

module must be importable by worker subprocesses. This means that ProcessPoolExecutor will not work in the interactive interpreter.

官网介绍:https://docs.python.org/3/library/concurrent.futures.html

concurrent.futures.ThreadPoolExecutor(可行方案)

唯一找到没有main()限制的可行方案

def load_data_parallel(b):

mean_image = read_image(join(data_dir, 'mean_frame.png'))

##————————###

with futures.ThreadPoolExecutor(max_workers=8) as excutor:

batch_list = list(range(batchsize))

excutor.map(load_data_parallel, batch_list)

官网介绍:https://docs.python.org/3/library/concurrent.futures.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值