If you find yourself engaged on initiatives that require exterior recordsdata in Google Colab, you might discover that importing recordsdata instantly out of your desktop into your Colab setting is sluggish. With that mentioned, there are two quicker choices for importing and accessing recordsdata in Colab: utilizing wget to retrieve recordsdata from an exterior location, and mounting your Google Drive to entry recordsdata there.
Utilizing both wget to obtain a file from an exterior location (i.e. a CDN, your web site, GitHub) or Google Drive comes with a major profit: as a result of the file is persistent, you possibly can entry it in a number of periods with out having to re-upload the file into Colab.
On this information, we’ll talk about tips on how to shortly add recordsdata into Google Colab with wget and Google Drive.
With out additional ado, let’s get began!
Choice #1: Mount Google Drive
You possibly can add recordsdata to Google Drive then obtain them into Google Colab. This lets you completely retailer your recordsdata within the cloud. Google Drive has an official integration with Colab. With a couple of traces of code, you possibly can mount and retrieve recordsdata from Google Drive in Colab.
Whilst you nonetheless have to add your recordsdata in your web connection, you solely want to do that as soon as. That is quicker than manually importing recordsdata into Colab, the place you have to add recordsdata each time you begin a brand new session.
First, add the recordsdata you wish to use in Colab notebooks to your Google Drive. We suggest importing the recordsdata in a folder to be able to mount the precise folder.
Right here is an instance of a folder known as bottles in a Google Drive. On this state of affairs, I’m loading a picture dataset into Colab to coach a laptop imaginative and prescient mannequin. Pc imaginative and prescient datasets might be many 1000’s of photographs which makes this integration particularly helpful.
To mount your Google Drive into Colab, create a brand new Colab cell and add the next code:
from google.colab import drive
from google.colab import recordsdata drive.mount("/content material/drive")
You possibly can solely mount your full Drive; you can not mount particular folders.
If you run this code, you’ll be requested to verify you wish to connect with Google Drive:
You’ll then be requested to grant the requisite permissions:
💡
Word: It is best to solely obtain recordsdata from Google Drive in case you belief the code within the pocket book, since granting permission offers learn and write entry to the recordsdata in Drive.
Then, run:
recordsdata.add()
This can obtain the recordsdata from the folder into your Google Colab setting, permitting you to make use of them in your pocket book.
For instance, we will open a file from our Drive utilizing:
from IPython.show import Picture
Picture('/content material/drive/bottles/photographs/instance.png')
Right here is the outcome:
We efficiently downloaded our recordsdata into Colab.
Choice #2: Add with wget
In case you have a file obtainable on an exterior web site – a CDN, for instance – you possibly can obtain the recordsdata into Google Colab with wget. That is quicker than importing recordsdata instantly into Google Colab as a result of Colab’s back-end can obtain the recordsdata instantly into the Colab back-end.
You possibly can obtain a file with wget utilizing the next code:
!wget https://instance.com
The place instance.com is the URL of the file that you’re downloading.
In case you have downloaded a zipper file, you possibly can unzip it with:
!unzip file.zip
Conclusion
On this information, we walked by way of two methods to shortly add recordsdata into Google Colab:
- Mount a Google Drive folder and obtain recordsdata from there, and;
- Obtain recordsdata from wget which can be obtainable from an exterior supply (i.e. a CDN).
If you’re desirous about utilizing Google Colab to coach laptop imaginative and prescient fashions, take a look at Roboflow Notebooks, a repository of over 40 tutorials that present tips on how to practice and work with varied laptop imaginative and prescient fashions. Notebooks covers state-of-the-art mannequin architectures from CLIP to SAM-2 to Florence-2.