How to predict multiple images in Keras at a time using multiple-processing (e.g. with different CPUs)?

后端 未结 2 658
时光取名叫无心
时光取名叫无心 2021-02-11 09:36

I have a lot of PNG images that I want to classify, using a trained CNN model.

To speed up the process, I would like to use multiple-processing with CPUs (I have 72 avai

2条回答
  •  眼角桃花
    2021-02-11 10:03

    One python package I know that may help you is joblib. Hope it can solve your problem.

    from joblib import Parallel, delayed
    
    # load model
    mymodel = load_model('190704_1_fcs_plotclassifier.h5')
    
    # Define callback function to collect the output in 'outcomes'
    outcomes = []
    
    def collect_result(result):
        global outcomes
        outcomes.append(result)
    
    # Define prediction function
    
    def prediction(img):
        img = cv2.resize(img,(49,49))
        img = img.astype('float32') / 255
        img = np.reshape(img,[1,49,49,3])       
    
        status = mymodel.predict(img)
        status = status[0][1]
    
        return(status)
    
    # Define evaluate function
    
    def evaluate(i,figure):
    
        # predict the propability of the picture to be in class 0 or 1
        img = cv2.imread(figure)
        status = prediction(img)
    
        outcome = [figure, status]
        return(i,outcome)
    
    outcomes = Parallel(n_jobs=72)(delayed(evaluate)(i,figure) for figure in listoffigurepaths)
    

提交回复
热议问题