简体   繁体   English

python内存密集脚本

[英]python memory intensive script

After about 4 weeks of learning, experimenting, etc. I finally have a script which does what I want. 经过大约4个星期的学习,实验等,我终于有了一个脚本,可以执行我想要的操作。 It changes the perspective of images according to a certain projection matrix I have created. 它根据我创建的某个投影矩阵来更改图像的透视图。 When I run the script for one image it works fine, however I would like to plot six images in one figure. 当我为一张图像运行脚本时,它可以正常工作,但是我想在一张图中绘制六张图像。 When I try to do this I get a memory error. 当我尝试执行此操作时,出现内存错误。

All the images are 2448px in width and 2048 px in height each. 所有图片的宽度均为2448像素,高度为2048像素。 My script: 我的剧本:

files = {'cam1': 'c1.jpg',
         'cam2': 'c2.jpg',
         'cam3': 'c3.jpg',
         'cam4': 'c4.jpg',
         'cam5': 'c5.jpg',
         'cam6': 'c6.jpg'}

fig, ax = plt.subplots()

for camname in files:
    img = Image.open(files[camname])
    gray_img = np.asarray(img.convert("L"))
    img = np.asarray(img)
    height, width, channels = img.shape

    usedP = np.array(P[camname][:,[0,1,3]])
    usedPinv = np.linalg.inv(usedP)
    U, V = np.meshgrid(range(gray_img.shape[1]),
                       range(gray_img.shape[0]))
    UV = np.vstack((U.flatten(),
                    V.flatten())).T
    ones = np.ones((UV.shape[0],1))
    UV = np.hstack((UV, ones))

    # create UV_warped
    UV_warped = usedPinv.dot(UV.T).T

    # normalize vector by dividing by the third column (which should be 1)
    normalize_vector = UV_warped[:,2].T
    UV_warped = UV_warped/normalize_vector[:,None]

    # masks
    # pixels that are above the horizon and where the V-projection is therefor positive (X in argus): make 0, 0, 1
    # pixels that are to far: make 0,0,1
    masks = [UV_warped[:,0]<=0, UV_warped[:,0]>2000, UV_warped[:,1]>5000, UV_warped[:,1]<-5000] # above horizon: => [0,0,1]
    total_mask = masks[0] | masks[1] | masks[2] | masks[3]
    UV_warped[total_mask] = np.array([[0.0, 0.0, 1.0]])

    # show plot
    X_warped = UV_warped[:,0].reshape((height, width))
    Y_warped = UV_warped[:,1].reshape((height, width))
    gray_img = gray_img[:-1, :-1]

    # add colors
    rgb = img[:,:-1,:].reshape((-1,3)) / 255.0 # we have 1 less faces than grid cells
    rgba = np.concatenate((rgb, np.ones((rgb.shape[0],1))), axis=1)

    plotimg = ax.pcolormesh(X_warped, Y_warped, img.mean(-1)[:,:], cmap='Greys')
    plotimg.set_array(None)
    plotimg.set_edgecolor('none')
    plotimg.set_facecolor(rgba)

ax.set_aspect('equal')
plt.show()

I have the feeling that numpy.meshgrid is quite memory intensive, but I'm not sure. 我觉得numpy.meshgrid占用大量内存,但是我不确定。 Does anybody see where my memory gets eaten away rapidly? 有人看到我的记忆迅速消失吗? (BTW, I have a laptop with 12Gb of RAM, which is only used by other programs for a very small part) (顺便说一句,我有一台具有12Gb RAM的笔记本电脑,只有一小部分被其他程序使用)

You might want to profile your code with this library . 您可能要使用此库来分析代码。

It will show you where your script is using memory. 它会告诉您脚本在哪里使用内存。

There is a Stackoverflow question here about memory profilers. 还有一个问题#1 有关内存分析器。 Also, I've used the trick in this answer in the past as a quick way to get an idea where in the code memory is going out of control. 另外,我过去使用此答案中的技巧作为快速了解代码存储器中失控位置的方法。 I just print the resource.getrusage() results all over the place. 我只是在各处打印resource.getrusage()结果。 It's not clean, and it doesn't always work, but it's part of the standard library and it's easy to do. 它不是很干净,并且不能总是工作,但是它是标准库的一部分,很容易做到。

I ordinarily profile with the profile and cProfile modules, as it makes testing individual sections of code fairly easy. 我通常使用profilecProfile模块进行profile cProfile ,因为它使测试代码的各个部分变得相当容易。

Python Profilers Python Profilers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM