Skip to content

Instantly share code, notes, and snippets.

@Mukei
Last active November 29, 2019 00:56
Show Gist options
  • Save Mukei/37b167a4821aac1320407b7ed2a3a591 to your computer and use it in GitHub Desktop.
Save Mukei/37b167a4821aac1320407b7ed2a3a591 to your computer and use it in GitHub Desktop.
How to use Kaggle in Google Colaboratory
#source https://www.kaggle.com/general/64962
!pip install kaggle
#Authenticating with Kaggle API
from googleapiclient.discovery import build
import io, os
from googleapiclient.http import MediaIoBaseDownload
from google.colab import auth
auth.authenticateuser() driveservice = build('drive', 'v3')
results = driveservice.files().list( q="name = 'kaggle.json'", fields="files(id)").execute() kaggleapikey = results.get('files', []) filename = "/content/.kaggle/kaggle.json" os.makedirs(os.path.dirname(filename), existok=True)
request = driveservice.files().getmedia(fileId=kaggleapikey[0]['id'])
fh = io.FileIO(filename, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
os.chmod(filename, 600)
#Create directories and prepare the data in colab
!mkdir /kaggleDogCat/
!mkdir /kaggleDogCat/models
!mkdir /kaggleDogCat/datasets
!mkdir /kaggleDogCat/datasets/dogs-vs-cats
#download the dataset from kaggle
import zipfile
!kaggle competitions download -c dogs-vs-cats -p /kaggleDogCat/datasets/dogs-vs-cats
os.chdir('/kaggleDogCat/datasets/dogs-vs-cats/')
#extract the images
for file in os.listdir():
if os.path.splitext(file)1==".zip":
zipref = zipfile.ZipFile(file, 'r') zipref.extractall()
zip_ref.close()
#create directories
!mkdir /kaggleDogCat/datasets/dogs-vs-cats/valid
!mkdir /kaggleDogCat/datasets/dogs-vs-cats/train/cats
!mkdir /kaggleDogCat/datasets/dogs-vs-cats/train/dogs
!mkdir /kaggleDogCat/datasets/dogs-vs-cats/valid/cats
!mkdir /kaggleDogCat/datasets/dogs-vs-cats/valid/dogs
!mkdir /kaggleDogCat/datasets/dogs-vs-cats/test1/predict
#copy images to the relevant class directory (e.g if the image name starts with cat so move it to 'cats' directory)
!mv /kaggleDogCat/datasets/dogs-vs-cats/train/cat.jpg /kaggleDogCat/datasets/dogs-vs-cats/train/cats/ !mv /kaggleDogCat/datasets/dogs-vs-cats/train/dog.jpg /kaggleDogCat/datasets/dogs-vs-cats/train/dogs/
#copy sample images to the validation directory
!cp /kaggleDogCat/datasets/dogs-vs-cats/train/cats/cat11.jpg /kaggleDogCat/datasets/dogs-vs-cats/valid/cats/
!cp /kaggleDogCat/datasets/dogs-vs-cats/train/dogs/dog11.jpg /kaggleDogCat/datasets/dogs-vs-cats/valid/dogs/
#creating a new layer is can be simple line in our code
you can check first, i think that you don't need to intall it
hera you have also the proper install steps
! pip install keras
! pip install numpy
! pip install pillow
#We are done with importing the dataset from Kaggle and saving it locally on colab platform
#import the tools that you need to work with
#such as:
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Conv2D, MaxPooling2D
#finally, upload the data and copty yous steps.
traindataset = ("/kaggleDogCat/datasets/dogs-vs-cats/train/") validdataset = ("/kaggleDogCat/datasets/dogs-vs-cats/valid/")
test_dataset = ("/kaggleDogCat/datasets/dogs-vs-cats/test1/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment