如何在Python中使用带有大量嵌套列表的Joblib Parallel类?

时间:2019-10-17 03:12:39

标签: python multiprocessing joblib

我有一个CSV文件,其中包含3列数据(名称,图像路径和索引)。我有一个名为 Do_Training()的函数,该函数从CSV文件读取,然后将CSV文件转换为一个大的嵌套列表(即:“ [[name1,path1,1],[ name2,path2,2],....] “)。

当我尝试将 Parallel()类(来自Joblib库)与 Delayed()方法一起使用时,我得到一个“无法腌制发送给工人的任务。” 错误。

当我删除 Delayed()方法时, Process_Training_Image()函数将按预期工作,直到到达嵌套列表的末尾,但最终会命中”无法解压缩不可迭代的NoneType对象” 异常。

Process_Training_Image()函数不应返回任何内容,但是为了避免异常,当我尝试从该函数返回值时,会遇到很多错误。

Do_Training()函数

#Train all images in a given CSV file of correct format
def Do_Training(csv_file_path):
    print("Starting Training...")
    start_time=time.time()

    with open(csv_file_path,'r') as csvFile:
        csvReader=csv.reader(csvFile,delimiter=',')
        csvLines=list(csvReader)

        process=Parallel(num_cores) ((Process_Training_Image)(row) for row in csvLines)


        #threads=[]
        #for name,path,idx in csvLines:
        #    #t= threading.Thread(target=Process_Training_Image,args=(name,path,idx))
        #    threads.append(t)

        #for t in threads:
        #    t.result()
        #for t in threads:
        #    t.join()

        end_time=time.time()
        print("Training Done!")
        print("Time Elapsed: {}".format(end_time-start_time))
        print("Number of Valid Training Images: {}".format(len(trFaceVectors)))

Process_Training_Image()函数

#Process Training Image
def Process_Training_Image(row):
    print("\nTraining on image: {} @ {}".format(row[0],row[1]))
    #Load Image From File
    image=cv2.imread(row[1])
    #Convert BGR to RGB
    face_image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    #Detect Faces from 'face_image'. Upscaling =1 
    det_faces=face_detector(face_image,1)
    #There should be atleast on Face.
    #Traning images has one face per image.
    if len(det_faces)==0:
        print("Face Image invalid or not found!")
    if len(det_faces)>0:
        for i,d in enumerate(det_faces):
            #Print Face Details
            print("Face Found! Left: {} Right: {} Top: {} Bottom: {}".format(d.left(),d.right(),d.top(),d.bottom()))
            #Detect Landmarks on Face
            landmarks=shape_predictor(face_image,d)
            #Compute Face_Descriptor Vector
            face_descriptor=face_recognizer.compute_face_descriptor(face_image,landmarks)
            #Append Lists
            trFaceNames.append(row[0])
            trFaceVectors.append(face_descriptor)
    #return {'a':row[0],'b':row[1],'c':row[2]}

应该处理所有图像,并填充所有相应列表,而不会出现任何错误。

0 个答案:

没有答案