我有一些基本上是numpy保存文件的数据集和标签,扩展名为.npy .
我在我的谷歌硬盘中保存了train.npy和train_labels.npy .
在使用Google Colab时,我必须使用该数据 . 我能够在驱动器中找到文件夹和数据文件的ID . 如何将这些数据文件加载到Google Colab使用的虚拟机内存中?
解决了它 .
首先执行doc中所述的简单身份验证
from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from google.colab import auth from oauth2client.client import GoogleCredentials # 1. Authenticate and create the PyDrive client. auth.authenticate_user() gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth)
我创建了一些帮助函数,如果你知道驱动器上存在文件的文件名和文件夹ID,它将获取文件ID . 文件夹ID是drive.google.com/../../folders/中链接的最后一部分
def get_file_from_drive(folder_id, file_name): file_list = drive.ListFile({'q': "'" + folder_id + "' in parents and trashed=false"}).GetList() for file in file_list: if file['title'] == file_name: return file['id'] def upload_file_to_drive(file_name, file_data): uploaded = drive.CreateFile({'title': file_name}) uploaded.SetContentString(file_data) uploaded.Upload() print('Uploaded file with ID {}'.format(uploaded.get('id'))) drive_folder_id = '<Folder ID>'
此功能将文件从谷歌驱动器上传到colab允许您使用的虚拟系统 .
def upload_data_system(): downloaded = drive.CreateFile({'id': get_file_from_drive(drive_folder_id, 'train.npy')}) downloaded.GetContentFile('train.npy') downloaded = drive.CreateFile({'id': get_file_from_drive(drive_folder_id, 'train_labels.npy')}) downloaded.GetContentFile('train_labels. upload_data_system()
中提琴!您的文件被上传到文件系统,可以使用简单的python加载到内存中,就像在本地完成一样 . 要验证,请在colab上运行 . 你应该看到你的文件
import os from os import listdir for f in os.listdir('.'): if os.path.isfile(f): print(f)
现在将numpy文件加载为np.load(path_to_file_in_filesystem)
1 回答
解决了它 .
首先执行doc中所述的简单身份验证
我创建了一些帮助函数,如果你知道驱动器上存在文件的文件名和文件夹ID,它将获取文件ID . 文件夹ID是drive.google.com/../../folders/中链接的最后一部分
此功能将文件从谷歌驱动器上传到colab允许您使用的虚拟系统 .
中提琴!您的文件被上传到文件系统,可以使用简单的python加载到内存中,就像在本地完成一样 . 要验证,请在colab上运行 . 你应该看到你的文件
现在将numpy文件加载为np.load(path_to_file_in_filesystem)