Python:数据预处理加速办法

2021-01-14 10:19:33 浏览数 (1)

预处理加速

  • 核心就是:多进程

例子

代码语言:javascript复制
import glob
import os
import cv2


### Loop through all jpg files in the current folder 
### Resize each one to size 600x600
for image_filename in glob.glob("*.jpg"):
  ### Read in the image data
  img = cv2.imread(image_filename)
  
  ### Resize the image
  img = cv2.resize(img, (600, 600)) 

改进方式:多cpu同时运行

代码语言:javascript复制
import glob
import os
import cv2
import concurrent.futures


def load_and_resize(image_filename):
  ### Read in the image data
  img = cv2.imread(image_filename)
  
  ### Resize the image
  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")

    ### Process the list of files, but spl

0 人点赞