在 google collab 中上传带标签的图片

Upload images with labels in google collab

我在 google 协作中使用 jupyter notebook。我的训练数据集如下所示:

/data/label1/img1.jpeg
.
.
.
/data/label2/img90.jpeg

我想导入这样的数据集。我尝试过的事情

第一步:

!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 是我在驱动器中的协作笔记本文件夹。我想阅读图像以及 labels.In 以执行相同的操作我正在使用代码:

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

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

PS:我仍然悬而未决这个问题,因为您可能会使用具有子文件夹名称作为训练图像标签的图像数据集到达此处,它适用于因此此处发布的解决方案适用于具有子文件夹的两个目录以及包含文件的目录。

您应该以递归方式上传您的数据集。 Here 是关于如何将数据集从 Google Drive 上传到 Colab

的示例