Python Pytorch线程时内存泄漏

Python Pytorch线程时内存泄漏,python,memory-leaks,pytorch,Python,Memory Leaks,Pytorch,在运行Pytorch模型评估数据集中的图像时,我遇到了严重的内存泄漏。 每个新的图像评估都在一个新线程中启动 代码是否等待线程完成并不重要。不使用线程时(只调用evaluate函数),不存在任何泄漏。我尝试在每次迭代中删除线程变量,但这没有帮助 代码如下: hidden_sizes = [6336, 1000] class Net(torch.nn.Module): def __init__ (self): super(Net, self).__init__()

在运行Pytorch模型评估数据集中的图像时,我遇到了严重的内存泄漏。 每个新的图像评估都在一个新线程中启动

代码是否等待线程完成并不重要。不使用线程时(只调用evaluate函数),不存在任何泄漏。我尝试在每次迭代中删除线程变量,但这没有帮助

代码如下:

hidden_sizes = [6336, 1000] 
class Net(torch.nn.Module):
    def __init__ (self):
        super(Net, self).__init__()
        self.fc1 = torch.nn.Linear(hidden_sizes[0], hidden_sizes[1])

    def forward(self, x):
        x= x.view(-1,hidden_sizes[0])
        x = torch.nn.functional.log_softmax(self.fc1(x), dim=1)
        return x
#---------------------------------------------------------------
def newThread(i):
   image=cv2.imread(pathx+filenames[i], cv2.IMREAD_GRAYSCALE)

   images = tran(image)
   images = tran1(images)
   images = images.unsqueeze(0)

   #Run model
   with torch.no_grad():
      logps = model(images)
   ps = torch.exp(logps)
   probab = list(ps.numpy()[0])
   pred_label = probab.index(max(probab))
#---------------------------------------------------------------
model = Net ()
model.load_state_dict(torch.load("test_memory_leak.pt"))
#normalize image
tran = transforms.ToTensor()
tran1 = transforms.Normalize((0.5,), (0.5,))
   
pathx="images\\"
filenames=os.listdir(pathx)

for i in range(len(filenames)):

    thread1  = threading.Thread(target = newThread, args = (i, )) 
    thread1.start()
    thread1.join()
原因可能是什么

UPD:试图用guppy检测内存泄漏,但原因仍然不清楚。以下是一些统计数据:第一个表是开始时的程序内存使用情况,第二个表是在分析1000个图像后,内存使用情况增加了x2.4倍(高达480mb):

Partition of a set of 260529 objects. Total size = 33587422 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  72188  28  9511536  28   9511536  28 str
     1  70921  27  5418336  16  14929872  44 tuple
     2  32926  13  2526536   8  17456408  52 bytes
     3  16843   6  2434008   7  19890416  59 types.CodeType
     4   2384   1  2199952   7  22090368  66 type
     5  14785   6  2010760   6  24101128  72 function
     6   4227   2  1631384   5  25732512  77 dict (no owner)
     7    794   0  1399928   4  27132440  81 dict of module
     8   2384   1  1213816   4  28346256  84 dict of type
     9     38   0   704064   2  29050320  86 dict of torch.tensortype
<575 more rows. Type e.g. '_.more' to view.>
-------------------------------------------

Partition of a set of 265841 objects. Total size = 34345930 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  72203  27  9523346  28   9523346  28 str
     1  70924  27  5418584  16  14941930  44 tuple
     2  32928  12  2530722   7  17472652  51 bytes
     3  16844   6  2434152   7  19906804  58 types.CodeType
     4   2384   1  2200488   6  22107292  64 type
     5  14786   6  2010896   6  24118188  70 function
     6   4232   2  1637736   5  25755924  75 dict (no owner)
     7    794   0  1399928   4  27155852  79 dict of module
     8   2384   1  1213816   4  28369668  83 dict of type
     9    265   0   840672   2  29210340  85 set
<577 more rows. Type e.g. '_.more' to view.>
一组260529对象的分区。总大小=33587422字节。
索引计数%Size%累计%Kind(类/类的目录)
0 72188 28 9511536 28 9511536 28街
170921 27 5418336 16 14929872 44元组
2 32926 13 2526536 8 17456408 52字节
3 16843 6 2434008 7 19890416 59类型。代码类型
4 2384 1 2199952 7 22090368 66型
5 14785 6 2010760 6 24101128 72功能
6 4227 2 1631384 5 25732512 77 dict(无所有者)
7 794 0 1399928 4 27132440 81模块目录
8 2384 1 1213816 4 28346256 84型dict
9 38 0 704064 2 29050320 86 torch.tensortype目录
-------------------------------------------
一组265841个对象的分区。总大小=34345930字节。
索引计数%Size%累计%Kind(类/类的目录)
0 72203 27 9523346 28 9523346 28街
170924 27 5418584 16 14941930 44元组
2 32928 12 2530722 7 17472652 51字节
3 16844 6 2434152 7 19906804 58类型。代码类型
4 2384 1 2200488 6 22107292 64型
5 14786 6 2010896 6 2418188 70功能
6 4232 2 1637736 5 25755924 75 dict(无所有者)
7 794 0 1399928 4 27155852 79模块目录
8 2384 1 1213816 4 28369668 83类型目录
9265 0840672 29210340 85套

您使用的是CPU还是GPU?同时评估了多少图像?我正在使用CPU。同时只计算一个图像(thread1.join()在循环中)。我甚至试图建立一个队列,以防止同时处理,但这也没有帮助。