Skip to content

Training using Kaggle kernel

Barata Magnus edited this page Oct 8, 2021 · 1 revision

This page will guide you on how to use this repository and train on Kaggle's kernel.

Preparation

The steps needed are similar to the preparation for creating a submission.

Training

Same with when you create a submission, the first step is to create a notebook with the competition dataset and the uploaded custom dataset. While most of the dependencies are covered by Kaggle's default environment, there might be additional dependencies you need to install. You can install those dependencies manually or using the command below.

!cat /kaggle/input/your-uploaded-dataset/kaggle_dataset/src/setup/requirements.txt | xargs -n 1 python3 -m pip install --no-cache-dir

There are two ways to actually train your model. The first one is by using IPython shell command (recommended), and the second one is by using this repo as a library and write your own training codes.

Using shell command (easy and recommended)

You can invoke shell command inside Jupyter's notebook by using the prefix !. Below is a sample code.

import json

params = {
  "name": "Sample hyperparameter",
  ...
  "data": {
    "dirname": "/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification",
    ...
  },
  ...
  "desc": "Sample hyperparameter file"
}

with open('default_params.json', 'w') as fp:
    json.dump(params, fp)

# Visualize using tensorboard (not supported / disabled for current version of Kaggle's Docker)
# %load_ext tensorboard
# %tensorboard --logdir exp-dir

# Invoke the shell command
!python /kaggle/input/your-uploaded-dataset/kaggle_dataset/src/train.py exp-dir

Here are some tips for when training using shell command:

  • If you face some errors during training, the fastest way to do a clean-up is by doing a factory reset (Run → Factory reset).
  • It is a good practice to do "dry run" (i.e. using a small number of epochs). After you find hyperparameters that work well, change back the number of epochs and stop your session. Then you can use the Save & Run All (Commit) to do the training session in the background.
  • After you find working hyperparameters to train your model, don't forget to commit the changes to the repo! 😉
  • If you ran out of RAM, uploading your processed dataset before training combined with writing a custom training codes (the second method below) might alleviate RAM usage.

Writing custom training codes

Same with when creating submission, you can use this repo as a library and write your own training codes.

import sys
sys.path.append('/kaggle/input/your-uploaded-dataset/kaggle_dataset/src')

# From here you can import this repo's codes just like you would normally do on a local environment
from train import data_prep, train
import utils
...

###  Your Training Code  ###
...

Using TPU

...