簡體   English   中英

如何正確地將數據從Google雲端硬盤導入Google Colab Notebook?

[英]How to properly import data from Google Drive to Google Colab Notebook?

我知道這很簡單,但是我需要一些指導。

我有一台Jupyter筆記本,該筆記本曾經在本地Linux機器上運行。 筆記本具有一些深度學習培訓代碼,可導入數據集,處理和培訓以及相關內容。

在我的本地計算機上,我的數據集位於

'/home/USERNAME/Workspace/Final Year Project/input'

輸入文件夾有兩個子文件夾訓練測試當我在本地計算機上運行筆記本計算機時,它可以完美運行,但是我的系統有一些限制,因此我選擇使用Google Colab。

但是我面臨的主要問題是如何在Colab中導入相同的數據集? 就像我知道可以使用Google雲端硬盤來完成操作一樣,但是怎么做呢?

目前,我正在使用文件路徑將數據集加載到numpy數組中

如果我將數據集上傳到Google雲端硬盤,該如何使用該文件路徑?

例如,要獲取訓練數據,我使用以下函數,該函數將文件路徑作為參數

# Get training data
def get_X_data(path, output_shape=(None, None)):
    '''
    Loads images from path/{id}/images/{id}.png into a numpy array
    '''
    img_paths = ['{0}/{1}/images/{1}.png'.format(path, id) for id in os.listdir(path)]
    X_data = np.array([skimage.transform.resize(skimage.io.imread(path)[:,:,:3], output_shape=output_shape, mode='constant', preserve_range=True) for path in img_paths], dtype=np.uint8)  #take only 3 channels/bands

    return X_data
X_train = get_X_data(train_path, output_shape=(img_height,img_width))

任何幫助將非常感激。 謝謝。

將數據集上傳到雲端硬盤后,您必須在此處使用此代碼

  • 在執行您擁有的代碼單元之前,請確保將此代碼塊復制到另一個單元中
  • 首先在單元格中運行此代碼!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

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
file_id = 'REPLACE_WITH_YOUR_FILE_ID'
downloaded = drive.CreateFile({'id': file_id})
print('Downloaded content "{}"'.format(downloaded.GetContentString()))

注意:對於Google雲端硬盤中的每個文件,您都有一個file_id,該文件名是該文件的可共享鏈接 ,而該鏈接的最后一部分,您將獲得文件ID。 文件ID類似於: laggVyWshwcyP6kEI-y_W3P8D26sz

  • 在代碼中替換此file_id

將文件同步到macOS或Window上的雲端硬盤的一種簡單方法是安裝雲端硬盤同步客戶端-https://www.google.com/drive/download/

然后,在Colab中,您可以使用以下命令掛載Drive文件:

from google.colab import drive
drive.mount('/content/drive')

之后,您的文件將出現在路徑/content/drive/My Drive和文件瀏覽器中,如下所示:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM