简体   繁体   中英

Why does my Google Colab Session run out of ram?

I have several images in a folder and i am trying to turn each one into grayscale and save them into another folder

Google Colab session keeps crashing due to running out of ram and i have tried using del on every variable

here is my code

img_array = []

for filename in FileArray:
    img = cv2.imread('train/train/Img-'+filename)
    height, width, layers = img.shape
    size = (width, height)
    img_array.append(img)

    image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    cv2.imwrite('train/gray/Img-'+filename, image)
    del img
    del height
    del width
    del layers
    del size
    del image

Even though you are deleting img , the image is still held in memory in the img_array list. If you have a lot of images in FileArray , you can very quickly chew through your RAM by keep them all in memory.

Try removing the line:

img_array.append(img)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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