Blog, Machine Learning, Python

How to Import Google Drive and Files to Google Colab — A Complete Guide for Beginners

One of my students ask me about this question, so I thought why not write a blog about it as it can help many beginners?

One way to use Google Colaboratory for deep learning projects is to clone a GitHub repository to your Google Drive. This will allow you to access the computational resources, such as a GPU, provided by Google Colab.

Before starting, it is important to have your scripts ready and properly organized within different folders according to their functions. For example, scripts that handle general utilities, file handling, accuracy metrics, and plotting can be placed in a folder called ‘utilities’. Similarly, scripts that define the deep learning model can be placed in a folder called ‘model’. Additionally, it is also a good practice to save your datasets as ‘.hdf5’ files and to include ‘datasets’ and ‘models’ folders within your project directory to store and save your datasets and deep learning models, respectively.

Once your scripts and datasets are ready, you can push your project folder (excluding the dataset files) to a GitHub repository. After this, you can create a new folder in your Google Drive called ‘project_folder’ to include all your project-related files and folders. To access the ‘project_folder’, right-click on the background and select ‘colaboratory’ from the ‘more’ option in the dropdown menu. This will open a Colab tab in your browser.

To connect to a GPU runtime, go to the ‘Runtime’ menu in the menubar, select ‘Reset All Runtimes’, and then select ‘Change Runtime Type’. In the dialog box that appears, select the ‘GPU’ option under the ‘hardware accelerator’ field and click ‘save’. Then, click the ‘connect’ button in the top right corner of the page to allocate, initialize and connect to a GPU runtime.

To mount your Google Drive in this runtime, you can run the following code in the first cell of your Colab:

from google.colab import drive
drive.mount('/content/gdrive')

This will prompt you with a URL and an authentication code. After inserting the code in the provided space, your Google Drive will be mounted. To check the contents of the current folder in the runtime, you can run !ls command.!

ls

Now you have successfully downloaded the repository to your google drive. You can also verify that the repository has been cloned to your google drive by going to your project_folder in the google drive and checking whether the repository exists in it.

Next, you can navigate to the project folder by typing the following in the cell and running it.

%cd {Your_Repo}

You can now run your scripts by typing in the following in a cell and running it.

! python {path_to_your_script}

For example, if your script is in the ‘src’ folder and is called ‘main.py’, you can run it by typing the following in a cell and running it.

! python src/main.py

Finally, you can clone the GitHub repository to your Google Drive by running the command

! git clone https://github.com/[username]/[repository-name].git

This will clone the entire repository, including the scripts, to the project.

Dall-E

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

#Python Programming

#Beginners Guide

#Machine Learning

#Google Colaboratory

#Towards Data Science

Leave a Reply