【发布时间】:2021-11-04 07:22:26
【问题描述】:
我是 Google Colab 的新手,我使用 joblib 存储的数据约为 18GiB。它无法在 Jupyter Lab 上运行,因为我的 PC 只有 8GB 内存,并且会返回 MemoryError。我求助于 Google Colab,刚刚订阅了 ColabPro 以获得 25GB 的 RAM,它终于加载了,但是当我想拆分数据时,它超过了 25GB。
我能想到的解决方案是让数据已经拆分到 joblib 文件中。但是当我使用 Google Colab 运行之前的预处理代码时,它需要的时间比使用 Jupyter Lab 在 localhost 上执行的时间要长。
2小时过去了,还是卡在一个牢房里。而 Jupyter Labs 需要 30 分钟来运行整个笔记本文件。
我知道 GPU 和 TPU 选项在训练复杂的神经网络模型时会更有用,但我认为与 Google Colab 相比,我的预处理笔记本在 Jupyter Lab localhost 中运行得更快是没有意义的。
我正在使用的数据集是我从 Kaggle 获得的 87,000 张 ASL 手势图像。需要很长时间运行的部分是将这些图像调整大小、转换和规范化为 NumPy 数组。
# define new function to load images from source folder
def create_dataset_gray(img_folder):
"""
read image file from the source folder and convert into the right color format, resize the image, convert image into a Numpy array with float as datatype
and Normalize image array to have values scaled down between 0 and 1.
"""
# create empty list to store image arrays as X, and label for each image as y
X=[]
y=[]
# iterate through the list of entries in the directory of specified folder
for dir1 in os.listdir(img_folder):
# print conversion progress of the code
print("Converting and Importing images in file {} ...".format(dir1))
# iterate through the images in the entries of the directory of specified folder
for img in os.listdir(os.path.join(img_folder, dir1)):
# set image path for each image
image_path = os.path.join(img_folder, dir1, img)
# utilise opencv package to read image from specified file
image = cv2.imread(image_path, cv2.COLOR_BGR2GRAY) # convert image into grayscale image
# resize image read
image = cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH), interpolation=cv2.INTER_AREA) # INTER_AREA is used when shrinking image is needed
# set image value datatype as float
image = image.astype("float")
# divide 255 to normalize image array to have values between 0 and 1
image /= 255
# append new image array and label into respective empty list
X.append(image)
y.append(dir1)
# set list as array for X and y
X = np.asarray(X)
y = np.asarray(y)
return X, y
这是需要永远运行的代码。
希望有人可以帮助您了解 Google Colab 的工作原理:
- 与在我的 PC 上运行相比,它不应该运行得更快吗?
- 是否有更好的解决方案来解决 RAM 问题?
【问题讨论】:
标签: python opencv out-of-memory google-colaboratory large-data