【问题标题】:How to load .npz file into Google Compute Engine如何将 .npz 文件加载到 Google Compute Engine
【发布时间】:2019-09-21 22:23:43
【问题描述】:

我有一个 CNN 模型,我想在连接到 Google Compute Engine 中的 VM 实例的 Jupyter 界面中运行。我一直无法弄清楚如何从 Jupyter 读取数据并将照片图像数据转换为保存在 Google Cloud Storage Bucket 中的 .npz 文件。

这是我迄今为止尝试过的:

def load_dataset():

    # load dataset

    data = load('gs://bucket/data.npz')
    X, y = data['arr_0'], data['arr_1']

    # separate into train and test datasets
    trainX, testX, trainY, testY = train_test_split(X, y, test_size=0.3, random_state=1)
    print(trainX.shape, trainY.shape, testX.shape, testY.shape)
    return trainX, trainY, testX, testY

我想我可以使用 gsutil 函数来为存储桶和文件的路径设置别名,但得到一个错误,说不存在这样的文件。

这是完整的回溯:

【问题讨论】:

  • 如果错误出现在load 语句中(显示完整消息和回溯是个好主意),那么其余代码是多余的。您需要专注于获取正确的路径和/或从源下载。
  • 请向我们提供您正在使用的load 函数的背景信息。

标签: numpy google-cloud-platform google-compute-engine jupyter


【解决方案1】:

我们可以建议您两种不同的方式。

一个是让 Python 将 gsutildownload a Cloud Storage object 调用到您的文件系统,然后使用该数据。

或者您可以使用Client Library,这可以说是一种更好的方法。

对于后者,请确保您过去在运行代码的机器上运行过 pip install google-cloud-storage

假设您使用的 load 函数将当前工作目录中的文件作为输入。

将以下 sn-p 添加到您的源代码中:

from google.cloud import storage

client = storage.Client()

def download_gcs_object(name_bucket, name_blob):
    bucket = client.bucket(name_bucket)
    blob = bucket.blob(name_blob)
    blob.download_to_filename(blob.name)
    print("Downloaded into current working directory a file with name ", blob.name)

在此之后,您可以通过这种方式编辑您发布的文章:

def load_dataset():

    #download a Cloud Storage object
    BUCKET="bucket" #TODO edit
    BLOB="data.npz" #TODO edit
    #or #BUCKET, BLOB = 'gs://bucket/data.npz'.split('/')[-2:] #if you prefer, have to edit accordingly again
    download_gcs_object(BUCKET, BLOB)

    # load dataset from filename with the blob name 

    data = load(BLOB)
    #The rest of the code is as it was...
    X, y = data['arr_0'], data['arr_1']

    # separate into train and test datasets
    trainX, testX, trainY, testY = train_test_split(X, y, test_size=0.3, random_state=1)
    print(trainX.shape, trainY.shape, testX.shape, testY.shape)
    return trainX, trainY, testX, testY

如果它不能解释 load 函数是什么,请告诉我们这是否有效。

【讨论】:

    猜你喜欢
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2015-09-27
    • 2018-09-01
    • 2014-07-10
    • 2020-04-30
    相关资源
    最近更新 更多