如何在colab中释放内存?

Reghunaath AA

我尝试遍历不同的超参数以构建最佳模型。但是在完成1次迭代(训练1个模型)之后,我在第2次迭代开始时内存不足。ResourceExhaustedError: OOM when allocating tensor with shape[5877,200,200,3] and type double on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc [Op:GatherV2]

我尝试使用,ops.reset_default_graph()但是它什么也没做。

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import regularizers
from tensorflow.keras.layers import Dense,Activation,Flatten,Conv2D,MaxPooling2D,Dropout
import os
import cv2
import random
import pickle
import time
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import TensorBoard
from google.colab import files
from tensorflow.python.framework import ops
p1=open("/content/tfds.pickle","rb")
def prepare_ds():
    dir="drive//My Drive//dataset//"
    cat=os.listdir(dir)
    i=1
    td=[]
    for x in cat:
        d=dir+x
        y1=cat.index(x)
        for img in os.listdir(d):
            im=cv2.imread(d+"//"+img)
            print(i)
            i=i+1     
            im=cv2.resize(im,(200,200))
            td.append([im,y1])
    ##      im[:,:,0],im[:,:,2]=im[:,:,2],im[:,:,0].copy()
    ##      plt.imshow(im)
    ##      plt.show()
    random.shuffle(td)
    X=[]
    Y=[]
    for a1,a2 in td:
        X.append(a1)
        Y.append(a2)
    X=np.array(X).reshape(-1,200,200,3)
    Y=np.array(Y).reshape(-1,1)
    pickle.dump([X,Y],p1)
##prepare_ds()
X,Y=pickle.load(p1)
X=X/255.0
def learn():
    model=tf.keras.models.Sequential()
    model.add(Conv2D(lsi,(3,3),input_shape=X.shape[1:]))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2,2)))

    for l in range(cli-1):
      model.add(Conv2D(lsi,(3,3)))
      model.add(Activation("relu"))
      model.add(MaxPooling2D(pool_size=(2,2)))

    model.add(Flatten())
    for l in range(dli):
      model.add(Dense(lsi))
      model.add(Activation("relu"))

    model.add(Dropout(0.5))
    model.add(Dense(10))
    model.add(Activation('softmax'))

    model.compile(loss="sparse_categorical_crossentropy",optimizer="adam",metrics=['accuracy'])
    model.fit(X,Y,batch_size=16,validation_split=0.1,epochs=3,verbose=2,callbacks=[tb])
    model.save('tm1.h5')
    ops.reset_default_graph()

dl=[0,1,2]
ls=[32,64,128]
cl=[1,2,3]
for dli in dl:
  for lsi in ls:
    for cli in cl:
      ops.reset_default_graph()
      NAME = "{}-conv-{}-nodes-{}-dense".format(cli, lsi, dli)
      tb=TensorBoard(log_dir="logs//{}".format(NAME))
      print(NAME)
      learn()

p1.close()
!zip -r /content/file.zip /content/logs
!cp file.zip "/content/drive/My Drive/"
达里安·谢特勒(Darien Schettler)

嗨,您好。

您可以在Python中使用内置的垃圾收集器库。我经常创建一个自定义回调,在每个时期的末尾使用此库。您可以将其视为清除不再需要的缓存信息

# Garbage Collector - use it like gc.collect()
import gc

# Custom Callback To Include in Callbacks List At Training Time
class GarbageCollectorCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        gc.collect()

此外,仅尝试单独运行命令gc.collect()即可查看结果并查看其工作方式。这是一些有关其工作原理的文档我经常用它来使我的内核尺寸在只有Kaggle比赛的内核中很小**


我希望这有帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档