Python 模块 DeepImageSearch 为多个搜索提供 ResourceExhaustedError

Python module DeepImageSearch gives ResourceExhaustedError for multiple searches

提问人:Andrei 提问时间:3/9/2023 更新时间:3/9/2023 访问量:116

问:

我正在尝试使用来自 9 个类的图像在医学数据库上实现 KMeans 算法。我从图像(类)的每个子文件夹中选择一个随机图像(质心),并将该图像放在质心的新文件夹中。之后,我想遍历所有图像并找出哪个是最近的 2 个质心。实现(python代码)如下:

import os
import shutil
import cv2
import numpy as np
import pandas as pd
from tkinter import *
from tkinter import filedialog
from DeepImageSearch import Index,LoadData,SearchImage

#create the folder in which the centroid images will be stored
pathCentroids="centroids"
if os.path.exists(pathCentroids):
    shutil.rmtree(pathCentroids)
os.makedirs(pathCentroids)

#open a file dialog to take the path to the main folder
root=Tk()
folderPath=filedialog.askdirectory()
root.destroy()

#select one random image from each subFolder(class) and puts it in the centroid folder
centroidNames=[]
subFolders=os.listdir(folderPath)
for subFolder in subFolders:
    
    sourcePath=os.path.join(folderPath,subFolder)
    imageNames=os.listdir(sourcePath)
    pos=np.random.randint(len(imageNames))
    centroidNames.append(imageNames[pos])
    
    source=os.path.join(sourcePath,imageNames[pos])
    destination=os.path.join(pathCentroids,imageNames[pos])
    shutil.copy(source,destination)
    
    
image_list=LoadData().from_folder(folder_list=[pathCentroids])
Index(image_list).Start()

#iterates over each subfolder(class)
data_for_frame=[]
for subFolder in subFolders:
    sourcePath=os.path.join(folderPath,subFolder)
    
    #iterates over each image from each class
    for imgName in os.listdir(sourcePath):
        if imgName in centroidNames:
            continue
            
        #compares the current image with the centroids
        imgPath=os.path.join(sourcePath,imgName)
        result=SearchImage().get_similar_images(image_path=imgPath,number_of_images=2)
        print(imgName)
        print(result)
        print()
        data_for_frame.append([imgName,result])
        
#save the results as a csv file    
dataFrame=pd.DataFrame(data_for_frame,columns=["imgName","top2SimilarityCentroids"])
dataFrame.to_csv("DeepImageSearchCentroids.csv",index=False)

问题出现在最后一个循环中,在成功识别了几次最接近的图像后 它突然给出一个错误,说所有资源都已用尽。

ResourceExhaustedError: {{function_node __wrapped__AddV2_device_/job:localhost/replica:0/task:0/device:GPU:0}} failed to allocate memory [Op:AddV2]

我正在使用 CPU 进行处理,但它一直工作到第 7 个图像。我想也给出错误的代码行以某种方式加载到内存中过多的数据。SearchImage().get_similar_images(image_path=imgPath,number_of_images=2)

Python 机器学习 图像处理 人工智能 K-手段

评论

0赞 jraufeisen 3/11/2023
错误消息告诉您 GPU 上的内存不足。因此,似乎并非所有代码都在 CPU 上执行

答: 暂无答案