执行python代码时内存消耗大

问题描述 投票:0回答:0

我有以下代码:

with open('vector.h5', 'wb') as f:
    f.close()

vector = pd.HDFStore('vector.h5', mode='r+')


with open('compare.h5', 'wb') as f:
    f.close()

compare = pd.HDFStore('compare.h5', mode='r+')
for idx, img in enumerate(IMAGE_TITLE):
    
    embedding_main = get_insightface_embedding(PATH_DIR + img) #512 numbers type float
    analyze = get_deepface_analyze(PATH_DIR + img) #string length 15
    embedding_second = get_face_recognition_embedding(PATH_DIR + img) #128 numbers type float

    vector[img] = pd.Series(np.concatenate([embedding_main['embedding'], 
                                            embedding_second,
                                            embedding_main['pose'],
                                            np.array([embedding_main['gender'], \ embedding_main['age'], analyze['dominant_race']])], axis=0))
    imgs_posted = vector.keys()
    
    array = []
    
    for img_vector in imgs_posted:

        metrics_vector1 = compare_vector_cosine(vector[img][:512], vector[img_vector][:512])
        
        array.append(metrics_vector1)
     
    compare[img] = pd.Series(data=np.array(array), index=imgs_posted)

为什么它使用大量内存?如何减少它的使用?

我需要优化比较一些图像,在第一个版本中我使用了一个大矩阵,但问题是它不适合内存。然后我决定使用 hdf5(我不熟悉它)将这些部分保存在硬盘上,但我的代码仍然无法以最佳方式运行,在比较 2000 张图像时花费了 9 GB 的 RAM。告诉我,可能是什么问题?我能做错什么?是否有可能使代码更加优化?

python-3.x pandas memory hdf5 ram
© www.soinside.com 2019 - 2024. All rights reserved.