【问题标题】:Could not load GCS dataset into my Colab Tensorflow ipynb无法将 GCS 数据集加载到我的 Colab Tensorflow ipynb
【发布时间】:2022-10-24 21:22:24
【问题描述】:

我刚开始使用 Google Colab 学习 Tensorflow,但我立即面临问题......

我想加载已存储在 Google Cloud Storage 中的现有个人数据集:

from google.colab import auth
auth.authenticate_user()
!gsutil ls gs://MY_BUCKET_NAME/

(其中 MY_BUCKET_NAME 实际上是“cloud-ai-platform-d7863b94-84f9 ...”)。这列出了该存储桶中的一些文件夹,例如“MyDataSet_normal”、“”MyDataSet_bad”,这意味着我的笔记本能够读取此 GCS 存储桶。

然后我跟着https://www.tensorflow.org/datasets/gcs 尝试加载数据集:
ds_train, ds_test = tfds.load(name="MyDataSet_normal", split=["train", "test"], data_dir="gs://MY_BUCKET_NAME", try_gcs=True),但它返回:

DatasetNotFoundError:未找到数据集 MyDataSet_normal。
可用数据集:
- 抽象推理
- 口音数据库
- AESLC
- ...

看起来它正试图在公开共享中找到“MyDataSet_normal”张量流数据集,而不是我自己在 MY_BUCKET_NAME 中的数据集。我尝试了谷歌,没有得到任何有用的信息。

我在这里想念什么?我如何告诉我的 Colab 笔记本,请查看 MY_BUCKET_NAME 的数据集,而不是公开的张量流数据集?

谢谢!

【问题讨论】:

  • 你检查过这个github link吗?
  • 谢谢@RoopaM,你的意思是我们不能溪流现有数据集,必须遵循您提供的链接,下载一个复制它,然后在下载的所有内容复制?

标签: tensorflow google-cloud-storage


【解决方案1】:

我曾尝试过 Google Cloud,您可以使用特定路径,但我的试用期已过,然后我使用免费的 Google Drive。

您可以使用 model.save 或对称方法,我将它们存储在数据库缓冲区中,但结果和保存的权重可以在安全驱动器中保持不变。 Google cloud 和 Google Collab 上有很多功能,但是对于数据集存储,Google Drive 就足够了,除了过滤器。

示例:终端距离远,可以节省催款成本。

import io
import os
from os.path import exists

from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools
from googleapiclient.http import MediaIoBaseDownload

import tensorflow as tf
import tensorflow_io as tfio
import matplotlib.pyplot as plt

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
n_folder = 50
encoding = "utf-8"
    
# define path variables
credentials_file_path = 'F:\temp\Python\credentials\credentials.json'
clientsecret_file_path = 'F:\temp\Python\credentials\client_secret_183167298301-pfhgtdf6k8r4918csmftemgk00ln8l4r.apps.googleusercontent.com.json'

# define API scope
SCOPE = 'https://www.googleapis.com/auth/drive'

# define store
store = file.Storage(credentials_file_path)
credentials = store.get()
# get access token
if not credentials or credentials.invalid:
    flow = client.flow_from_clientsecrets(clientsecret_file_path, SCOPE)
    credentials = tools.run_flow(flow, store)
    
# define API service
http = credentials.authorize(Http())
drive = discovery.build('drive', 'v3', http=http)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Fuctions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def download_file( file_id, filename, filetype ): 
    print( 'downfile: ' + filename + ': ' + filetype )

    request = drive.files().get_media( fileId=file_id )
    file = io.BytesIO()
    downloader = MediaIoBaseDownload( file, request )
    done = False

    if filetype == "application/vnd.google-apps.folder":
        return


    try:
        while done is False:
            status, done = downloader.next_chunk()
            print( F'Download {int(status.progress() * 100)}.' )
        
    except HttpError as error:
        print(F'An error occurred: {error}')
        file = None
    
    tf.io.write_file(
        filename, file.getvalue(), name='write_file'
    )

    return 

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Write result to file
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
file = 'F:\datasets\downloads\Actors\train\Pikaploy\01.tif'
image = tf.io.read_file( file )
image = tfio.experimental.image.decode_tiff(image, index=0)
image = tf.image.resize(image, [8,8], method='nearest')

filename='F:\temp\datasets\9.tif'

with open( filename, "wb" ) as f:
    b = bytes(str(image), encoding='utf-8')
    f.write(b)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Read result to file
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
temp = tf.io.read_file(
    filename, name='dataset_9'
)
temp = tf.io.decode_raw(
    temp, tf.uint8, little_endian=True, fixed_length=None, name=None
)
temp = tf.constant(temp, shape=(1, 8, 8, 3))

输出:

tf.Tensor(
[[[133 141 126 255]
  [ 94 107  90 255]
  [106 125  97 255]
  [141 140 122 255]
  [ 96 114  90 255]
  [ 88 106  82 255]
  [112 141  93 255]
  [116 127 111 255]]

 ...

 [[150 122 111 255]
  [180 152 141 255]
  [192 160 145 255]
  [185 153 138 255]
  [168 148 139 255]
  [189 158 138 255]
  [166 136 110 255]
  [ 68  83  64 255]]], shape=(8, 8, 4), dtype=uint8)

【讨论】:

  • 谢谢!你的意思是在GCS中没有办法使用数据集,你必须先下载它?在您的代码中,我看不到您如何访问 Google Drive 中的数据集,download_file 未被任何语句调用。
  • ?? 我已经回答了这个问题并告诉了备用选项,因为我的帐户试用期已过期。
  • ?? 将数据集视为结果⁉️ 这样您就可以在安全的云存储中压缩算法,您需要访问才能运行它。
  • 谢谢,也许我遗漏了一些东西,但是从您的代码中,我看到您基本上是从本地 file = 'F:\datasets\...filename='F:\temp\datasets\9.tif' 访问一些文件,所以它不是直接从 GCS 或 Google Drive 读取的,对吗?
  • ?? 正确的是,我的实验存储在云存储中,当所有客户端访问时,他们拥有相同的数据和资源,然后他们可以继续单独工作,因为告诉数据集很大,你需要使用过滤器和许可证。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2020-06-22
  • 1970-01-01
  • 2018-07-20
相关资源
最近更新 更多