Downloading Data from Google Drive Colab

Viewed 2264

I'm a beginner in TensorFlow and python in general, so any help would be much appreciated. I'm following this tutorial from tensorflow, just with my own data.

So I'm trying to download my own data from a link I got from a folder that I uploaded to google drive. I will then use that data in an image classifier model. However, I start to see the images getting downloaded and it says:

dataset_url_training = "https://drive.google.com/drive/folders/genericid?usp=sharing" 
data_dir_training = tf.keras.utils.get_file('flower_photos', origin=dataset_url_training, untar=True)
data_dir_training = pathlib.Path(data_dir_training)

Downloading data from https://drive.google.com/drive/folders/genericid?usp=sharing

106496/Unknown - 2s 14us/step

And then it just stops. And when I try to use the following code:

print(image_count)

The output spits out: 0

I'm really confused and I don't know what to do. Some suggestions have been to make a zip file url, but that only applies to individual files and doesn't work for whole folders like mine. Furthermore, as far as I know, Google Drive doesn't allow you to get links for zip files, just those for sharing (they are my own files, for clarification).

Thank you.

Edit 1: Just want to be clear: I'm NOT looking for a path. I'm looking for a URL, hence the use of a directory. I've also tried using the. link of a zip file, but I got the same error message as before.

1 Answers

When you want to use a file from google drive in colab you can mount our drive to colab.

from google.colab import drive

drive.mount('/content/gdrive')

Than you can open files form google drive. For example your file is in the directory "folder" on your main drive page:

path = "gdrive/My Drive/folder/flower_photos"

Edit/Addition:

To make it more clear, you change this part from the tutorial

import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

to this

import pathlib
from google.colab import drive
drive.mount('/content/gdrive')
data_dir = "gdrive/My Drive/flower_photos"
data_dir = pathlib.Path(data_dir)
Related