首页 文章

如何将许多文件上传到Google Colab?

提问于
浏览
5

我正在研究image segmentation machine learning project,我想在Google Colab上测试一下 .

对于训练数据集,我有700张图像,主要是 256x256 ,我需要上传到我的项目的python numpy数组中 . 我还有上千个相应的掩码文件 . 它们目前存在于Google Cloud 端硬盘上的各种子文件夹中,但无法上传到Google Colab以便在我的项目中使用 .

到目前为止,我一直尝试使用谷歌保险丝,它似乎上传速度非常慢,PyDrive给我带来了各种身份验证错误 . 我大部分时间都在使用Google Colab I / O示例代码 .

我该怎么办呢? PyDrive会成为可行的方式吗?是否有代码用于一次上传文件夹结构或许多文件?

4 回答

  • 0

    您可以将所有数据放入google驱动器,然后装入驱动器 . 这就是我做到的 . 让我逐步解释 .

    Step 1: 将您的数据传输到您的谷歌硬盘 .

    Step 2: 运行以下代码以挂载谷歌硬盘 .

    # Install a Drive FUSE wrapper.
    # https://github.com/astrada/google-drive-ocamlfuse
    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
    !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
    !apt-get update -qq 2>&1 > /dev/null
    !apt-get -y install -qq google-drive-ocamlfuse fuse
    
    
    
    # Generate auth tokens for Colab
    from google.colab import auth
    auth.authenticate_user()
    
    
    # Generate creds for the Drive FUSE library.
    from oauth2client.client import GoogleCredentials
    creds = GoogleCredentials.get_application_default()
    import getpass
    !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
    vcode = getpass.getpass()
    !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
    
    
    # Create a directory and mount Google Drive using that directory.
    !mkdir -p My Drive
    !google-drive-ocamlfuse My Drive
    
    
    !ls My Drive/
    
    # Create a file in Drive.
    !echo "This newly created file will appear in your Drive file list." > My Drive/created.txt
    

    Step 3: 运行以下行以检查是否可以在装入的驱动器中看到所需的数据 .

    !ls Drive
    

    Step 4:

    现在将数据加载到numpy数组中,如下所示 . 我的exel文件包含我的火车和简历以及测试数据 .

    train_data = pd.read_excel(r'Drive/train.xlsx')
    test = pd.read_excel(r'Drive/test.xlsx')
    cv= pd.read_excel(r'Drive/cv.xlsx')
    

    我希望它可以提供帮助 .

    Edit

    要从colab笔记本环境将数据下载到驱动器中,可以运行以下代码 .

    # Install the PyDrive wrapper & import libraries.
    # This only needs to be done once in a notebook.
    !pip install -U -q PyDrive
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    from google.colab import auth
    from oauth2client.client import GoogleCredentials
    
    
    
    # Authenticate and create the PyDrive client.
    # This only needs to be done once in a notebook.
    auth.authenticate_user()
    gauth = GoogleAuth()
    gauth.credentials = GoogleCredentials.get_application_default()
    drive = GoogleDrive(gauth)
    
    
    
    # Create & upload a file.
    uploaded = drive.CreateFile({'data.xlsx': 'data.xlsx'})
    uploaded.SetContentFile('data.xlsx')
    uploaded.Upload()
    print('Uploaded file with ID {}'.format(uploaded.get('id')))
    
  • 0

    以下是将大型数据集上传到Google Colab的几个步骤

    1.上传您的数据集以释放 Cloud 存储,如dropbox,openload等(我使用了dropbox)
    2.创建上传文件的可共享链接并进行复制 .
    3.在Google Colab中打开笔记本,然后在其中一个单元格中运行此命令:

    !wget your_shareable_file_link
    

    而已!
    您可以使用以下命令在zip或rar文件中压缩数据集,然后在Google Colab中下载后将其解锁:

    !unzip downloaded_filename -d destination_folder
    
  • 2

    您可能想尝试 kaggle-cli 模块,如here所述

  • 6

    首先压缩文件,然后将其上传到Google Cloud 端硬盘 .

    看到这个简单的命令解压缩:

    !unzip {file_location}
    

    例:

    !unzip drive/models.rar
    

相关问题