【问题标题】:100% RAM usage in google colab谷歌 colab 中 100% 的 RAM 使用率
【发布时间】:2023-03-06 10:39:01
【问题描述】:

我目前正在将 google colab 用于我的手语识别模型深度学习项目之一,我正在加载我从 Google Drive 创建的自定义数据集。我的数据集包含不同的字母文件夹,其中包含各个字母的符号。

这只是我用来创建训练数据的代码的一部分

training_data = []
def create_training_data():
  for category in CATEGORIES:  
    path = os.path.join(DATADIR,category)  # create path to image of respective alphabet
    class_num = CATEGORIES.index(category)  # get the classification  for each alphabet A : 0, C : 1, D : 2,...

    for img in tqdm(os.listdir(path)):  # iterate over each image
      img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)  # convert to array
      training_data.append([img_array, class_num])  # add this to our training_data

create_training_data()

X = []
y = []

for features,label in training_data:
  X.append(np.array(features))
  y.append(label)


但只是这个过程占用了所有可用的 RAM,那么我有什么办法可以最大限度地减少 RAM 使用量??

【问题讨论】:

    标签: python google-colaboratory


    【解决方案1】:

    它会占用所有可用的 RAM,因为您只需将所有数据复制到其中。

    使用 PyTorch 中的 DataLoader 并定义批处理的大小(因为不是一次使用所有数据)可能更容易。

    import torch
    import torchvision
    from torchvision import transforms
    
    train_transforms = transforms.Compose([
        # transforms.Resize((256, 256)), # might also help in some way, if resize is allowed in your task
        transforms.ToTensor() ])
    
    train_dir = '/path/to/train/data/'
    
    train_dataset = torchvision.datasets.ImageFolder(train_dir, train_transforms)
    
    batch_size = 32
    train_dataloader = torch.utils.data.DataLoader( train_dataset, batch_size=batch_size )
    

    然后,在训练阶段,您可以执行以下操作:

    # ...
    for inputs, labels in tqdm(train_dataloader):
        inputs = inputs.to(device)
        labels = labels.to(device)
        # ...
    

    【讨论】:

      【解决方案2】:

      我无法复制您的训练数据集,因此请谨慎对待。如果您使用生成器生成训练数据而不是将其构建为列表,那么您应该消除一半的内存使用量。您仍然需要支付 X 和 Y 的内存成本,因此这种技术可能不足以解决您的问题。

      def iter_training_data():
        for category in CATEGORIES:  
          path = os.path.join(DATADIR,category)  # create path to image of respective alphabet
          class_num = CATEGORIES.index(category)  # get the classification  for each alphabet A : 0, C : 1, D : 2,...
      
          for img in tqdm(os.listdir(path)):  # iterate over each image
            img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)  # convert to array
            yield [img_array, class_num]
      
      X = []
      y = []
      
      for features,label in iter_training_data():
        X.append(np.array(features))
        y.append(label)
      

      【讨论】:

      • 因此同样适用于特征和标签。
      猜你喜欢
      • 2021-01-26
      • 2017-03-07
      • 2019-08-22
      • 2020-03-27
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 1970-01-01
      相关资源
      最近更新 更多