π How to Connect Kaggle with Google Colab and Download Datasets! π₯
Table of contents
- Step 1: Create a Kaggle Account and Generate API Token π
- Step 2: Upload Kaggle API Key to Colab π
- Step 3: Set Permissions to Secure Your API Key π
- Step 4: Download Dataset from Kaggle to Colab πΎ
- Step 5: Extract the Downloaded Dataset π¦
- Step 6: Verify Your Dataset π―
- And Thatβs It! You're All Set! π
- Happy Coding! π₯³π»
Connecting Kaggle with Google Colab is super easy! By following the steps below, you can download datasets directly into your Colab environment and start working on your machine learning projects right away. π
Step 1: Create a Kaggle Account and Generate API Token π
Sign in to Kaggle: If you don't already have a Kaggle account, head over to Kaggle and create one.
Generate API Token: Go to your Kaggle account settings by clicking on your profile picture. Scroll down to the "API" section and click "Create New API Token." This will download a file named
kaggle.json
containing your credentials.
Step 2: Upload Kaggle API Key to Colab π
Open Google Colab: Start a new notebook in Google Colab.
Upload
kaggle.json
: Use the code snippet below to create a Kaggle directory and upload the API key file.!mkdir -p ~/.kaggle !cp kaggle.json ~/.kaggle/
Step 3: Set Permissions to Secure Your API Key π
Set the permissions to make sure your API key is safe and not readable by others.
!chmod 600 ~/.kaggle/kaggle.json
Step 4: Download Dataset from Kaggle to Colab πΎ
With everything set up, you can now download any Kaggle dataset using the Kaggle API! π
Hereβs how to download a dataset, for example, the Face Mask Detection dataset:
!kaggle datasets download -d wobotintelligence/face-mask-detection-dataset
Step 5: Extract the Downloaded Dataset π¦
Most datasets come in compressed format. Use the following code to unzip the dataset:
import zipfile
zip_ref = zipfile.ZipFile('face-mask-detection-dataset.zip', 'r')
zip_ref.extractall('/content')
zip_ref.close()
Step 6: Verify Your Dataset π―
Finally, check if the dataset has been extracted correctly by listing the files in your working directory.
!ls /content
And Thatβs It! You're All Set! π
Now you can start exploring the dataset and building amazing machine learning models. Connecting Kaggle to Google Colab is this easy and powerful, letting you utilize the cloud resources for your projects.
Happy Coding! π₯³π»
Subscribe to my newsletter
Read articles from Nischal Baidar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by