How to Upload Image Folders in Google Colab

by Joyita Bhattacharya | posted on June 14, 2023


Step 1: Locate and zip the folder/ folders to be loaded on your computer

Step 2: Upload the zipped file/ files to your Google Drive

Click the 'New' button on the top-left corner of the drive page and upload the zipped file.

Step 3: Open Google Colab

Import relevant libraries

import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator

Step 4: Mount Google Drive

This is required so that Colab can access the required zipped file from Drive.

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

Once you run the above code, Google Colab will ask for permission to access your Google Drive files. Click on the connect/ allow button to grant access.

After authorization, you will find 'My Drive' mounted on the Colab file system, displayed on the file-explorer pane on the left.

Step 5: Load and extract the zipped file

Import the inbuilt python module called zipfile.

import zipfile
import os

To load the zipped file, copy the path. A screenshot for the same is shown below.

Now, extract all files into the /tmp folder of the Google Colab.

Step 6: Load the image folder for the machine learning project

Load the image folder for training using the ImageDataGenerator of Keras.

Click here to get the code.