【问题标题】:How can I stop my Colab notebook from crashing while normalising my images?如何在标准化图像时阻止我的 Colab 笔记本崩溃?
【发布时间】:2020-07-19 07:09:21
【问题描述】:

我正在尝试制作一个能够识别人类情绪的模型。我的代码和内存一开始就很好:

但是当我尝试规范化我的图像时,RAM 急剧上升

然后 Colab 就崩溃了:


这是导致 colab 崩溃的代码块:

import os
import matplotlib.pyplot as plt
import cv2

data = []

for emot in os.listdir('./data/'):
    for file_ in os.listdir(f'./data/{emot}'):
        img = cv2.imread(f'./data/{emot}/{file_}', 0)
        img = cv2.bitwise_not(img)
        img /= 255.0 # <--- This is the line that causes colab to crash
        data.append([img, emotions.index(emot)])

如果我删除 img /= 255.0,它不会崩溃,但是我有未标准化的图像!:
我什至尝试在另一个块中对其进行规范化:

for i in range(len(data)):
    data[i][0] = np.array(data[i][0]) / 255.0

但它不起作用并且仍然崩溃

【问题讨论】:

  • 您始终可以选择将一些预处理移到您的模型训练管道中,您使用的是哪个深度学习/ML 框架?
  • 我正在使用Tensorflow
  • 另外,什么是模型训练管道?对不起,我只是一个初学者,所以我对这一切都很陌生
  • 添加“data”列表的可能大小以及“img”的大小。

标签: python machine-learning crash google-colaboratory


【解决方案1】:

我想通过一个例子。首先让我们看一下下面的代码。

import numpy as np
x = np.random.randint(0, 255, size=(100, 32, 32), dtype=np.int16)

print('Present data type', x.dtype)
# What you did
y = x/255
print('Present data type', y.dtype)
# What you should do
z = (x/255).astype(np.float16)
print('Present data type', z.dtype)

输出:

Present data type int16
Present data type float64
Present data type float16

如果你仔细观察,当我划分x 变量并声明y=x/255 时,数据类型更改为float64。如果你划分 NumPy 数组的 int 数据类型,默认情况下,它被类型转换为 float64。通常,“float64”包含更大的内存。因此,在划分int 类型的 NumPy 矩阵时,对于较大的数据集,应始终将其转换为较短的数据类型。

如果您执行的代码在没有img /= 255.0 块的情况下流畅运行,那么就是这种情况。除法后,您应该将img 变量类型转换为可能的最低float 类型,例如np.float16np.float32。但是,np.float16 有一些限制,TensorFlow 不完全支持(TF 将其转换为 32 位浮点数),您可以使用np.float32 数据类型。

因此,请尝试在img /= 255.0 行之后添加img.astype(np.float16)img.astype(np.float32)

给出修改后的代码,

import os
import matplotlib.pyplot as plt
import cv2

data = []

for emot in os.listdir('./data/'):
    for file_ in os.listdir(f'./data/{emot}'):
        img = cv2.imread(f'./data/{emot}/{file_}', 0)
        img = cv2.bitwise_not(img)
        img = (img/255.0).astype(np.float16) # <--- This is the suggestion
        data.append([img, emotions.index(emot)])

【讨论】:

  • 好的,谢谢您的回复。一旦我有时间,我会检查这是否有效。再次非常感谢! ?
  • 我已经尝试过了,它有效! RAM 跳得很少,代码完全运行,非常感谢!但是,出现了另一个问题,现在每当我尝试使用 matplotlib 显示图像时,它都会显示ValueError: Unsupported dtype。那么我需要将其类型转换回float32 或其他什么吗?
  • 好的,我把它改成了float32,它现在可以工作了。非常感谢!
  • 如果您使用np.float16 数据类型,面对ValueError: Unsupported dtype 是一个常见问题(对于matplotlib 或任何其他库)。正如我在答案中提到的,np.float16 没有得到广泛支持(甚至处理器也不直接支持它)。所以,大多数情况下你应该坚持np.float32。如果您只面临太多内存问题,请切换到np.float16。参考:stackoverflow.com/questions/38975770/…
【解决方案2】:

假设管道的下一步是从图像语料库中创建一个tf.data.Dataset 对象,您可以使用Dataset.map() 将预处理移至数据加载管道以节省内存空间。 Tensorflow 有一个非常详细的指南,说明如何在此处执行此操作 -> https://www.tensorflow.org/guide/data#preprocessing_data

【讨论】:

  • 感谢您的回复,但我从未使用过tf.data.Dataset,所以它对我来说是新的。我试图调查它,但我遇到了一些错误(OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.,当我试图将我的函数映射到Dataset 对象时)。这个问题也出现在 Kaggle 上,请问是我的代码有问题吗?
  • 我需要一些代码才能提供更多帮助。但是,我强烈建议您在发布您遇到的每个问题之前遵循Tensorflow 教程,他们会比我更好地解释它。话虽如此,我怀疑您的Tensorflow 版本给您带来了问题,也许这会有所帮助-> stackoverflow.com/questions/59308263/…
  • 好的,谢谢你的建议,我会复习整个 tensorflow 教程:)
猜你喜欢
  • 2019-09-10
  • 2015-08-20
  • 2019-10-28
  • 1970-01-01
  • 2023-01-03
  • 1970-01-01
  • 2018-09-26
  • 2023-03-26
  • 2015-03-01
相关资源
最近更新 更多