Jupyter notebook 在google collab中上载带有标签的图像

Jupyter notebook 在google collab中上载带有标签的图像,jupyter-notebook,google-colaboratory,pydrive,Jupyter Notebook,Google Colaboratory,Pydrive,我正在谷歌合作中使用jupyter笔记本。我的培训数据集如下所示: /data/label1/img1.jpeg . . . /data/label2/img90.jpeg 我想导入这样的数据集。我试过的东西 步骤1: !pip install -U -q PyDrive %matplotlib inline import matplotlib import matplotlib.pyplot as plt from os import walk import os from pydrive.

我正在谷歌合作中使用jupyter笔记本。我的培训数据集如下所示:

/data/label1/img1.jpeg
.
.
.
/data/label2/img90.jpeg
我想导入这样的数据集。我试过的东西

步骤1:

!pip install -U -q PyDrive
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
from os import walk
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
步骤2:

# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
步骤3

file_to_download = os.path.expanduser('./data/')
file_list = drive.ListFile(
    {'q': 'id_of_the_data_directory'})
我不知道下一步该怎么做。文件夹
data
是驱动器中的“我的协作笔记本”文件夹。我想阅读带有标签的图像。为了做到这一点,我使用以下代码:

filename_queue=tf.train.string_input_producer(tf.train.match_filenames_once('data/*/*.jpeg'))
image_reader=tf.WholeFileReader()
key,image_file=image_reader.read(filename_queue)
#key is the entire path to the jpeg file and we need only the subfolder as the label
S = tf.string_split([key],'\/')
length = tf.cast(S.dense_shape[1],tf.int32)
label = S.values[length-tf.constant(2,dtype=tf.int32)]
label = tf.string_to_number(label,out_type=tf.int32)
#decode the image
image=tf.image.decode_jpeg(image_file)
#then code to place labels and folders in corresponding arrays

首先我想说的是,我们不能直接访问该文件夹。我们需要设置挂载点,所有驱动器内容都可以通过该点访问。多亏了这个 完全按照上面给出的答案链接中给出的步骤进行操作。但请确保根据创建的新驱动器文件夹更改路径


PS:我仍然没有回答这个问题,因为您可能会使用带有子文件夹名称的图像数据集作为培训图像的标签,因此此处发布的解决方案适用于带有子文件夹的目录以及带有文件的目录。

您应该以递归方式上载数据集。这是一个关于如何将数据集从Google Drive上传到Colab的示例