Skip to content

Instantly share code, notes, and snippets.

@saiashirwad
Created June 15, 2019 19:35
Show Gist options
  • Save saiashirwad/db9b84caaf2c8e90d020d8f951768cda to your computer and use it in GitHub Desktop.
Save saiashirwad/db9b84caaf2c8e90d020d8f951768cda to your computer and use it in GitHub Desktop.
Basic Colab Setup
# to be set
use_kaggle = False
competition_name = ""
base_folder = 'fastai_v3/'
from google.colab import drive
drive.mount('/content/gdrive/', force_remount=True)
root_dir = '/content/gdrive/My Drive/'
base_dir = root_dir + base_folder
% reload_ext
autoreload
% autoreload 2
% matplotlib
inline
# for older fastai courses
# !pip install fastai==0.7.0
# otherwise
!curl -s https://course.fast.ai/setup/colab | bash
# to get Kaggle data
import os
def get_kaggle_credentials():
token_dir = os.path.join(os.path.expanduser("~"), ".kaggle")
token_file = os.path.join(token_dir, "kaggle.json")
if not os.path.isdir(token_dir):
os.mkdir(token_dir)
try:
with open(token_file, 'r') as f:
pass
except IOError as no_file:
try:
from google.colab import files
except ImportError:
raise no_file
uploaded = files.upload()
if "kaggle.json" not in uploaded:
raise ValueError("You need an API key! see: "
"https://github.com/Kaggle/kaggle-api#api-credentials")
with open(token_file, "wb") as f:
f.write(uploaded["kaggle.json"])
os.chmod(token_file, 600)
def load_data_from_zip(competition, file):
with zipfile.ZipFile(os.path.join(competition, file), "r") as zip_ref:
unzipped_file = zip_ref.namelist()[0]
zip_ref.extractall(competition)
def get_data(competition):
kaggle.api.competition_download_files(competition, competition)
load_data_from_zip(competition, 'train.zip')
load_data_from_zip(competition, 'train_masks.zip')
load_data_from_zip(competition, 'train_masks.csv.zip')
if use_kaggle:
get_kaggle_credentials()
import kaggle
get_data(competition_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment