Loading

ADDI Alzheimers Detection Challenge

FastAI Tabular Starter

Minimal submission making predictions using fastai's tabular learner

Johnowhitaker

A short demo notebook getting an entry in with FastAI. It doesn't do very well - there is no adjusting for class baalance, no feature engineering, no dealing with missing values... just a basic starting point.

FastAI Tabular Starter Notebook by @johnowhitaker

Drawing

What is the notebook about?

The challenge is to use the features extracted from the Clock Drawing Test to build an automated and algorithm to predict whether each participant is one of three phases:

1) Pre-Alzheimer’s (Early Warning) 2) Post-Alzheimer’s (Detection) 3) Normal (Not an Alzheimer’s patient)

In this starter notebook we will solve this task using fastai. Make sure you don't edit out the section headings as AICROWD uses these to split this notebook up for submission. All code that is needed for both train and test goes in the preprocessing section, for example. I've tried to highlight where I've added code that isn't in the original template.

  • Installing packages. Please use the Install packages 🗃 section to install the packages
  • Training your models. All the code within the Training phase ⚙️ section will be skipped during evaluation. Please make sure to save your model weights in the assets directory and load them in the predictions phase section

Setup AIcrowd Utilities 🛠

We use this to bundle the files for submission and create a submission on AIcrowd. Do not edit this block.

In [1]:
!pip install -q -U aicrowd-cli
In [2]:
%load_ext aicrowd.magic

AIcrowd Runtime Configuration 🧷

Define configuration parameters. Please include any files needed for the notebook to run under ASSETS_DIR. We will copy the contents of this directory to your final submission file 🙂

The dataset is available under /ds_shared_drive on the workspace.

In [3]:
import os

# Please use the absolute for the location of the dataset.
# Or you can use relative path with `os.getcwd() + "test_data/validation.csv"`
AICROWD_DATASET_PATH = os.getenv("DATASET_PATH", "/ds_shared_drive/validation.csv")
AICROWD_PREDICTIONS_PATH = os.getenv("PREDICTIONS_PATH", "predictions.csv")
AICROWD_ASSETS_DIR = "assets"
AICROWD_API_KEY = "" # Get your key from https://www.aicrowd.com/participants/me

Install packages 🗃

Please add all pacakage installations in this section

In [4]:
!pip install -q numpy pandas scikit-learn
!pip install -q -U fastcore fastai # Need -U otherwise we're stuck with an old version on their docker

Define preprocessing code 💻

The code that is common between the training and the prediction sections should be defined here. During evaluation, we completely skip the training section. Please make sure to add any common logic between the training and prediction sections here.

Import common packages

Please import packages that are common for training and prediction phases here.

In [5]:
import numpy as np
import pandas as pd
from sklearn.metrics import f1_score, log_loss
from fastai.tabular.all import *

Training phase ⚙️

You can define your training code here. This sections will be skipped during evaluation.

In [6]:
# Loading the training data
df = pd.read_csv(os.getenv("DATASET_PATH", "/ds_shared_drive/train.csv"))
print(df.shape)
df.head()
(32777, 122)
Out[6]:
row_id number_of_digits missing_digit_1 missing_digit_2 missing_digit_3 missing_digit_4 missing_digit_5 missing_digit_6 missing_digit_7 missing_digit_8 ... bottom_area_perc left_area_perc right_area_perc hor_count vert_count eleven_ten_error other_error time_diff centre_dot_detect diagnosis
0 S0CIXBKIUEOUBNURP 12.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.526170 0.524975 0.474667 0 0 0 1 -105.0 0.0 normal
1 IW1Z4Z3H720OPW8LL 12.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.000810 0.516212 0.483330 0 1 0 1 NaN NaN normal
2 PVUGU14JRSU44ZADT 12.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.488109 0.550606 0.449042 0 0 0 0 0.0 0.0 normal
3 RW5UTGMB9H67LWJHX 7.0 0.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 ... NaN NaN NaN 1 0 0 1 NaN NaN normal
4 W0IM2V6F6UP5LYS3E 12.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.512818 0.511865 0.487791 0 1 0 0 0.0 1.0 normal

5 rows × 122 columns

Following the example in the fastai docs, we construct our dataloaders:

In [7]:
splits = RandomSplitter(valid_pct=0.2)(range_of(df))
to = TabularPandas(df.fillna(0), procs=[Categorify, FillMissing,Normalize],
                   cat_names = ['intersection_pos_rel_centre'],
                   cont_names = list(df.drop(['row_id', 'intersection_pos_rel_centre', 'diagnosis'], axis=1).columns),
                   y_names='diagnosis',
                   splits=splits)
dls = to.dataloaders(bs=64)
dls.show_batch()
intersection_pos_rel_centre number_of_digits missing_digit_1 missing_digit_2 missing_digit_3 missing_digit_4 missing_digit_5 missing_digit_6 missing_digit_7 missing_digit_8 missing_digit_9 missing_digit_10 missing_digit_11 missing_digit_12 1 dist from cen 10 dist from cen 11 dist from cen 12 dist from cen 2 dist from cen 3 dist from cen 4 dist from cen 5 dist from cen 6 dist from cen 7 dist from cen 8 dist from cen 9 dist from cen euc_dist_digit_1 euc_dist_digit_2 euc_dist_digit_3 euc_dist_digit_4 euc_dist_digit_5 euc_dist_digit_6 euc_dist_digit_7 euc_dist_digit_8 euc_dist_digit_9 euc_dist_digit_10 euc_dist_digit_11 euc_dist_digit_12 area_digit_1 area_digit_2 area_digit_3 area_digit_4 area_digit_5 area_digit_6 area_digit_7 area_digit_8 area_digit_9 area_digit_10 area_digit_11 area_digit_12 height_digit_1 height_digit_2 height_digit_3 height_digit_4 height_digit_5 height_digit_6 height_digit_7 height_digit_8 height_digit_9 height_digit_10 height_digit_11 height_digit_12 width_digit_1 width_digit_2 width_digit_3 width_digit_4 width_digit_5 width_digit_6 width_digit_7 width_digit_8 width_digit_9 width_digit_10 width_digit_11 width_digit_12 variance_width variance_height variance_area deviation_dist_from_mid_axis between_axis_digits_angle_sum between_axis_digits_angle_var between_digits_angle_cw_sum between_digits_angle_cw_var between_digits_angle_ccw_sum between_digits_angle_ccw_var sequence_flag_cw sequence_flag_ccw number_of_hands hand_count_dummy hour_hand_length minute_hand_length single_hand_length clockhand_ratio clockhand_diff angle_between_hands deviation_from_centre hour_proximity_from_11 minute_proximity_from_2 hour_pointing_digit actual_hour_digit minute_pointing_digit actual_minute_digit final_rotation_angle ellipse_circle_ratio count_defects percentage_inside_ellipse pred_tremor double_major double_minor vertical_dist horizontal_dist top_area_perc bottom_area_perc left_area_perc right_area_perc hor_count vert_count eleven_ten_error other_error time_diff centre_dot_detect diagnosis
0 TR 7.0 7.057206e-09 1.000000e+00 1.000000e+00 -5.494328e-09 1.000000e+00 1.000000e+00 -1.379354e-09 1.000000e+00 1.000000e+00 1.000000e+00 5.271532e-09 9.749793e-10 388.067654 -0.000016 336.513367 392.532807 -0.000012 -0.000008 356.753756 0.000007 0.000009 402.937039 0.000011 0.000009 3.816375e+01 -1.623396e-07 -8.599167e-07 1.217065e+01 7.745677e-07 -9.010474e-07 9.147690e+01 8.965976e-07 -2.088769e-07 2.309995e-07 8.345723e+01 2.041000e+01 2079.000003 0.000033 0.000025 3400.000017 -0.000129 -0.000133 7049.999927 0.000148 0.000050 -0.000185 7476.000001 6119.999999 77.000001 0.000002 0.000003 68.000000 0.000001 -0.000002 94.000000 0.000003 0.000002 -8.663088e-08 84.000000 72.000000 2.700000e+01 0.000001 9.035186e-07 5.000000e+01 -0.000001 -4.953110e-07 7.500000e+01 -0.000002 -0.000001 9.289842e-07 8.900000e+01 8.500000e+01 560.809516 91.285717 4.405781e+06 20.410000 -0.000007 0.000034 0.000006 20069.337550 -2.099637e-07 13897.909912 -2.372236e-08 -7.225576e-11 2.0 2.0 71.517014 79.516227 4.695936e-07 1.111850e+00 7.999214e+00 82.841530 1.889435e+01 1.343731e+02 6.161664e-07 4.000000e+00 11.0 1.000000e+00 2.0 330.000007 84.504089 120.999999 1.0 1.043564e-08 123.237007 111.294937 114.269546 119.529099 0.487391 0.512270 0.489199 0.510444 -3.986990e-08 1.101867e-08 8.023168e-10 1.000000e+00 424.999997 -2.806957e-09 normal
1 TL 12.0 7.057206e-09 -4.687249e-09 5.833634e-10 -5.494328e-09 5.513658e-09 -2.742456e-09 -1.379354e-09 1.969123e-09 2.469271e-09 -1.542272e-09 5.271532e-09 9.749793e-10 382.205448 376.367186 357.268097 367.848146 373.159824 355.035918 354.864043 345.231808 362.810210 370.113830 362.865326 377.641357 9.922331e+01 9.702140e+01 9.217000e+01 9.209540e+01 8.975448e+01 9.425000e+01 9.620657e+01 9.434489e+01 9.802000e+01 9.767731e+01 9.284990e+01 9.555000e+01 1611.999999 3419.999988 4819.000001 4696.999995 4679.999940 5655.999996 5580.000025 7101.999999 5076.000013 5111.999997 4306.999996 5628.000021 25.999999 57.000000 61.000000 61.000000 60.000000 56.000001 60.000001 67.000000 54.000000 7.200000e+01 59.000000 84.000000 6.200000e+01 60.000000 7.900000e+01 7.700000e+01 78.000000 1.010000e+02 9.300000e+01 106.000000 93.999999 7.100000e+01 7.300000e+01 6.700000e+01 228.992430 180.749999 1.803177e+06 94.997497 359.999999 21.914959 360.000005 4.436348 -2.099637e-07 4.436361 1.000000e+00 -7.225576e-11 2.0 2.0 81.792212 82.660781 4.695936e-07 1.010619e+00 8.685693e-01 77.467835 5.365383e+00 8.299478e+01 8.330142e+01 2.000000e+00 11.0 1.100000e+01 2.0 89.999999 86.778656 141.999999 1.0 1.043564e-08 118.552757 112.114525 112.218727 118.429917 0.487843 0.511814 0.522952 0.476689 2.000000e+00 2.000000e+00 8.023168e-10 1.000000e+00 495.000002 -2.806957e-09 normal
2 TL 11.0 7.057206e-09 -4.687249e-09 5.833634e-10 -5.494328e-09 5.513658e-09 -2.742456e-09 -1.379354e-09 1.969123e-09 2.469271e-09 1.000000e+00 5.271532e-09 9.749793e-10 372.349972 -0.000016 414.007845 369.914858 387.847897 356.508756 371.209167 373.471558 386.805174 369.943909 361.686674 364.888001 6.842238e+00 8.477510e+00 6.500003e-01 1.779404e+00 8.193443e-01 1.209000e+01 3.641623e+01 2.758651e+01 2.769000e+01 2.309995e-07 2.568404e+00 6.760001e+00 2775.000005 5220.000028 5480.999988 3249.999997 4080.000051 8215.999935 4424.999998 5165.999997 6665.999934 -0.000185 3402.999958 5952.000002 75.000000 58.000000 63.000000 65.000000 60.000000 103.999999 75.000000 82.000000 100.999999 -8.663088e-08 41.000001 62.000000 3.700000e+01 90.000000 8.700000e+01 5.000000e+01 68.000000 7.900000e+01 5.900000e+01 63.000000 66.000000 9.289842e-07 8.300000e+01 9.600000e+01 328.818177 351.072724 2.606021e+06 11.797500 359.999999 202.959712 360.000005 46.053321 -2.099637e-07 46.053309 1.000000e+00 -7.225576e-11 2.0 2.0 69.542587 95.625228 4.695936e-07 1.375060e+00 2.608264e+01 89.895163 1.358143e+01 2.421688e+00 2.082647e+00 1.100000e+01 11.0 2.000000e+00 2.0 -0.000002 89.308403 124.999999 1.0 1.043564e-08 124.454651 115.522758 116.380135 123.407807 0.498257 0.501413 0.541189 0.458472 1.000000e+00 1.101867e-08 8.023168e-10 -2.590800e-08 0.000003 -2.806957e-09 normal
3 BR 12.0 7.057206e-09 -4.687249e-09 5.833634e-10 -5.494328e-09 5.513658e-09 -2.742456e-09 -1.379354e-09 1.969123e-09 2.469271e-09 -1.542272e-09 5.271532e-09 9.749793e-10 329.013671 362.246460 342.791842 354.779144 290.594635 282.800293 297.578979 324.600127 333.061553 367.848480 386.099092 387.632875 1.472617e+01 2.299582e+01 1.820000e+01 1.296526e+01 1.017249e+01 9.620000e+00 6.600593e+00 1.753890e+01 2.743000e+01 2.496226e+01 2.012399e+01 6.110000e+00 2880.000024 7614.000142 6448.000054 6425.999976 4697.999973 7616.000014 4946.999995 6405.000033 6509.999947 10355.000189 7703.999912 10349.999833 90.000000 94.000001 104.000000 118.999999 86.999999 112.000001 97.000000 105.000000 92.999999 1.090000e+02 107.000000 90.000001 3.200000e+01 81.000000 6.200000e+01 5.400000e+01 54.000000 6.800000e+01 5.100000e+01 61.000000 70.000000 9.500000e+01 7.200000e+01 1.150000e+02 471.719700 103.174246 4.645284e+06 15.340000 359.999999 135.573334 360.000005 27.872966 -2.099637e-07 27.873213 1.000000e+00 -7.225576e-11 2.0 2.0 56.809258 76.744056 4.695936e-07 1.350908e+00 1.993480e+01 83.996803 4.906938e+00 3.900712e+00 3.396935e-01 1.100000e+01 11.0 2.000000e+00 2.0 -0.000002 86.682136 113.000000 1.0 1.043564e-08 116.988930 113.964157 114.157181 116.781204 0.471709 0.527943 0.526619 0.473026 -3.986990e-08 1.000000e+00 8.023168e-10 -2.590800e-08 0.000003 -2.806957e-09 normal
4 0 4.0 1.000000e+00 -4.687249e-09 1.000000e+00 1.000000e+00 5.513658e-09 -2.742456e-09 1.000000e+00 1.969123e-09 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 -0.000007 -0.000016 0.000008 -0.000004 387.863708 -0.000008 0.000004 355.163696 367.663574 0.000003 488.382014 0.000009 -4.083468e-07 1.033109e+01 -8.599167e-07 9.329410e-07 1.548192e+01 2.535000e+01 6.618926e-07 1.317185e+01 -2.088769e-07 2.309995e-07 -8.440965e-07 -4.351725e-07 -0.000073 4278.000000 0.000025 -0.000003 9645.999997 6099.000001 0.000092 10980.000112 0.000050 -0.000185 -0.000024 -0.000041 0.000001 69.000000 0.000003 -0.000002 106.000001 107.000000 -0.000001 122.000000 0.000002 -8.663088e-08 -0.000001 0.000004 5.203749e-07 62.000000 9.035186e-07 -2.481910e-07 91.000001 5.700000e+01 -8.320788e-07 89.999998 -0.000001 9.289842e-07 3.691041e-07 1.960336e-07 324.666656 508.666655 9.602766e+06 25.350001 359.999999 0.000034 360.000005 4200.252966 -2.099637e-07 4200.252972 1.000000e+00 -7.225576e-11 1.0 1.0 -0.000002 -0.000002 5.988867e+01 -3.305128e-08 4.112339e-07 -0.000002 4.916907e-07 3.676375e-07 6.161664e-07 -2.302025e-07 11.0 -4.815603e-08 2.0 30.000001 65.330147 27.000000 0.5 1.000000e+00 117.801903 96.366028 99.852333 112.190826 0.487255 0.512415 0.534312 0.465308 -3.986990e-08 1.101867e-08 8.023168e-10 1.000000e+00 0.000003 -2.806957e-09 normal
5 BL 10.0 7.057206e-09 -4.687249e-09 5.833634e-10 -5.494328e-09 1.000000e+00 -2.742456e-09 -1.379354e-09 1.969123e-09 2.469271e-09 -1.542272e-09 5.271532e-09 1.000000e+00 399.835262 357.452790 371.039427 -0.000004 399.163179 350.591277 355.442325 0.000007 369.493226 362.801604 358.589262 355.398711 1.034993e+02 5.698383e+01 9.113000e+01 9.240423e+01 7.745677e-07 7.813000e+01 9.370048e+01 9.204131e+01 9.204000e+01 9.243190e+01 9.645039e+01 -4.351725e-07 1673.999998 4366.000020 4601.999998 5100.000003 -0.000129 2360.000028 3248.000029 4153.999956 5130.000012 5486.999998 3792.000009 -0.000041 27.000000 74.000000 59.000000 50.999999 0.000001 39.999999 58.000000 62.000000 54.000000 5.900000e+01 48.000000 0.000004 6.200000e+01 59.000000 7.800000e+01 1.000000e+02 -0.000001 5.900000e+01 5.600000e+01 67.000000 95.000001 9.300000e+01 7.900000e+01 1.960336e-07 275.511108 165.955551 1.547337e+06 87.100000 359.999999 4099.556616 0.000006 8492.290954 -2.099637e-07 8492.291019 -2.372236e-08 -7.225576e-11 2.0 2.0 43.097305 81.933005 4.695936e-07 1.901117e+00 3.883571e+01 123.152811 3.017619e+01 5.899984e+00 6.772767e+01 1.100000e+01 11.0 1.000000e+00 2.0 270.000002 84.183266 86.000000 1.0 1.000000e+00 116.017754 112.073730 112.481918 115.570000 0.512272 0.487372 0.501274 0.498357 -3.986990e-08 2.000000e+00 8.023168e-10 1.000000e+00 5.000001 -2.806957e-09 normal
6 0 12.0 7.057206e-09 -4.687249e-09 5.833634e-10 -5.494328e-09 5.513658e-09 -2.742456e-09 -1.379354e-09 1.969123e-09 2.469271e-09 -1.542272e-09 5.271532e-09 9.749793e-10 340.586640 381.932252 343.005828 287.865416 322.172698 337.926027 379.962161 371.121615 348.213286 385.188262 424.714596 421.867860 8.835715e+01 8.325915e+01 8.762000e+01 9.878448e+01 9.606289e+01 9.008999e+01 9.977315e+01 1.091577e+02 1.077700e+02 9.626873e+01 8.569965e+01 7.475000e+01 2320.000012 6006.000079 4199.999997 7625.999949 5670.000003 6434.999984 4300.000000 3912.999990 5884.999974 5761.999999 7344.000022 7055.000011 29.000000 66.000000 49.999999 62.000000 70.000000 55.000001 50.000000 43.000001 55.000000 6.700000e+01 68.000000 83.000000 8.000000e+01 90.999999 8.400000e+01 1.230000e+02 81.000000 1.170000e+02 8.600000e+01 91.000001 107.000001 8.600000e+01 1.080000e+02 8.500000e+01 219.719698 203.787876 2.495732e+06 90.057501 359.999999 78.736178 360.000005 28.399661 -2.099637e-07 28.399776 1.000000e+00 -7.225576e-11 1.0 1.0 -0.000002 -0.000002 5.585887e+01 -3.305128e-08 4.112339e-07 -0.000002 4.916907e-07 3.676375e-07 6.161664e-07 -2.302025e-07 11.0 -4.815603e-08 2.0 89.999999 86.563255 133.999998 1.0 1.043564e-08 124.332870 111.164688 111.999260 123.194054 0.486095 0.513569 0.529518 0.470121 1.000000e+00 1.101867e-08 8.023168e-10 1.000000e+00 0.000003 -2.806957e-09 normal
7 TL 12.0 7.057206e-09 -4.687249e-09 5.833634e-10 -5.494328e-09 5.513658e-09 -2.742456e-09 -1.379354e-09 1.969123e-09 2.469271e-09 -1.542272e-09 5.271532e-09 9.749793e-10 336.823396 371.930788 393.137390 390.354003 287.003906 330.524201 353.571073 368.188143 394.457854 402.317352 380.020050 369.980072 1.383823e+00 1.054491e+00 6.760000e+00 3.864072e+00 5.621238e+00 4.939999e+00 4.641594e+00 1.740082e+01 1.573000e+01 2.626225e+01 3.226898e+01 9.880000e+00 1512.000004 2940.000004 3008.000015 3525.000002 4614.999942 3952.000031 2368.000065 3647.999965 3975.999996 5004.999972 3247.999966 4248.000030 54.000000 60.000000 64.000000 75.000000 71.000000 76.000000 64.000000 64.000000 71.000000 6.500000e+01 58.000000 59.000000 2.800000e+01 49.000000 4.700000e+01 4.700000e+01 65.000000 5.200000e+01 3.700000e+01 57.000000 56.000000 7.700000e+01 5.600000e+01 7.200000e+01 189.174240 47.901518 9.430663e+05 9.327500 359.999999 89.277595 0.000006 40.607904 -2.099637e-07 40.608056 1.000000e+00 -7.225576e-11 2.0 2.0 70.433128 75.036019 4.695936e-07 1.065351e+00 4.602887e+00 103.103928 1.034278e+01 1.167725e+02 1.034589e+02 2.000000e+00 11.0 1.100000e+01 2.0 -0.000002 87.813721 128.000001 1.0 1.043564e-08 123.061050 113.467613 115.299324 120.841278 0.468104 0.531559 0.550006 0.449645 2.000000e+00 1.000000e+00 8.023168e-10 1.000000e+00 495.000002 -2.806957e-09 normal
8 TL 12.0 7.057206e-09 -4.687249e-09 5.833634e-10 -5.494328e-09 5.513658e-09 -2.742456e-09 -1.379354e-09 1.969123e-09 2.469271e-09 -1.542272e-09 5.271532e-09 9.749793e-10 381.000337 407.092437 410.879853 424.338590 370.991244 394.562106 410.708218 430.976800 443.288854 442.280731 446.995516 426.281890 1.259551e+01 4.876071e+00 1.820000e+00 1.749263e+00 1.661820e+00 4.160001e+00 5.744986e+00 1.067385e+01 4.030001e+00 2.865699e-01 1.068499e+01 1.274000e+01 924.000006 1023.000134 1781.999964 2645.999954 1759.999899 2268.000024 2051.999945 2520.000087 2622.000012 3065.999958 9888.000033 2725.999986 33.000000 32.999999 54.000000 63.000000 44.000000 54.000000 57.000000 60.000000 57.000000 7.300000e+01 102.999998 47.000000 2.800000e+01 31.000000 3.300000e+01 4.200000e+01 39.999999 4.200000e+01 3.600000e+01 42.000000 46.000000 4.200000e+01 9.600000e+01 5.800000e+01 321.878785 350.272735 5.461694e+06 5.687501 359.999999 26.318962 360.000005 12.091147 -2.099637e-07 12.091336 1.000000e+00 -7.225576e-11 2.0 2.0 43.025875 84.883132 4.695936e-07 1.972839e+00 4.185725e+01 90.381790 9.641726e+00 9.686746e+00 3.237302e-01 1.100000e+01 11.0 2.000000e+00 2.0 -0.000002 86.134194 122.000001 1.0 1.043564e-08 119.203537 117.759552 117.785362 119.176781 0.460290 0.539371 0.547368 0.452285 2.000000e+00 1.000000e+00 8.023168e-10 -2.590800e-08 0.000003 -2.806957e-09 normal
9 TR 10.0 7.057206e-09 -4.687249e-09 5.833634e-10 -5.494328e-09 5.513658e-09 -2.742456e-09 -1.379354e-09 1.969123e-09 2.469271e-09 1.000000e+00 5.271532e-09 1.000000e+00 382.774746 -0.000016 312.829834 -0.000004 362.094606 281.113861 338.801574 329.203948 287.312103 338.003693 315.778412 313.737549 9.035760e+00 2.596090e+01 6.500001e+00 1.688457e+01 2.168551e+01 1.131000e+01 2.221207e+01 1.371167e+01 1.742000e+01 2.309995e-07 5.279425e+00 -4.351725e-07 4293.000007 5487.000071 9899.999919 9848.999955 8249.999955 12936.000257 7205.000061 8023.999923 13831.999646 -0.000185 15197.000224 -0.000041 81.000000 93.000002 150.000002 146.999998 124.999999 167.999999 131.000000 117.999999 152.000001 -8.663088e-08 167.000003 0.000004 5.300000e+01 59.000000 6.600000e+01 6.700000e+01 66.000000 7.700000e+01 5.500000e+01 68.000000 91.000001 9.289842e-07 9.100000e+01 1.960336e-07 178.455557 869.288872 1.283722e+07 11.743334 359.999999 2088.618890 360.000005 139.517727 -2.099637e-07 139.517629 1.000000e+00 -7.225576e-11 2.0 2.0 59.602981 61.294414 4.695936e-07 1.028378e+00 1.691434e+00 87.987358 2.276699e+01 9.629422e+01 8.241591e+01 2.000000e+00 11.0 1.100000e+01 2.0 -0.000002 89.734757 122.999999 1.0 1.043564e-08 117.858253 112.248657 117.583061 112.487999 0.495489 0.504158 0.524950 0.474689 1.000000e+00 1.101867e-08 8.023168e-10 1.000000e+00 495.000002 -2.806957e-09 normal

With these done, we can create a learner and train it for a short while.

In [8]:
learn = tabular_learner(dls, metrics=accuracy)
learn.fit_one_cycle(5)
epoch train_loss valid_loss accuracy time
0 0.371079 0.234524 0.948284 00:05
1 0.179927 0.187727 0.951030 00:06
2 0.172685 0.183211 0.951030 00:05
3 0.160957 0.183933 0.951030 00:06
4 0.150052 0.182679 0.951640 00:05

We're getting a log loss of 0.17 on our validation set, which is a random sub-sample of train. However, we should really look at the score on the provided validation set, which more closely matches the test set:

In [9]:
val = pd.read_csv(os.getenv("DATASET_PATH", "/ds_shared_drive/validation.csv"))
val = pd.merge(val, pd.read_csv(os.getenv("DATASET_PATH", "/ds_shared_drive/validation_ground_truth.csv")), 
               how='left', on='row_id')
print(val.shape)
test_dl = learn.dls.test_dl(val.fillna(0))
probs, y = learn.get_preds(dl=test_dl)
print('Log Loss on provided validation set:', log_loss(val['diagnosis'].values, probs.numpy()))
(362, 122)
Log Loss on provided validation set: 0.7788187272655996

Saving the trained model is as easy as:

In [10]:
learn.export(AICROWD_ASSETS_DIR + '/fai_model1.mdl')

Prediction phase 🔎

Please make sure to save the weights from the training section in your assets directory and load them in this section

In [11]:
learn = load_learner(AICROWD_ASSETS_DIR + '/fai_model1.mdl') # load the model

Load test data

In [12]:
test_data = pd.read_csv(AICROWD_DATASET_PATH)
test_data.head()
Out[12]:
row_id number_of_digits missing_digit_1 missing_digit_2 missing_digit_3 missing_digit_4 missing_digit_5 missing_digit_6 missing_digit_7 missing_digit_8 ... top_area_perc bottom_area_perc left_area_perc right_area_perc hor_count vert_count eleven_ten_error other_error time_diff centre_dot_detect
0 LA9JQ1JZMJ9D2MBZV 11.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.500272 0.499368 0.553194 0.446447 0 0 0 1 NaN NaN
1 PSSRCWAPTAG72A1NT 6.0 1.0 1.0 0.0 1.0 1.0 0.0 0.0 0.0 ... 0.572472 0.427196 0.496352 0.503273 0 1 0 1 NaN NaN
2 GCTODIZJB42VCBZRZ 11.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 ... 0.494076 0.505583 0.503047 0.496615 1 0 0 0 0.0 0.0
3 7YMVQGV1CDB1WZFNE 3.0 1.0 0.0 1.0 0.0 1.0 1.0 1.0 1.0 ... 0.555033 0.444633 0.580023 0.419575 0 1 0 1 NaN NaN
4 PHEQC6DV3LTFJYIJU 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 ... 0.603666 0.395976 0.494990 0.504604 0 0 0 1 150.0 0.0

5 rows × 121 columns

Generate predictions

In [13]:
test_dl = learn.dls.test_dl(test_data.fillna(0)) # New dataloader with the test data
preds, _ = learn.get_preds(dl=test_dl)
preds = preds.numpy() # Convert to numpy array 
predictions = {
    "row_id": test_data["row_id"].values,
    "normal_diagnosis_probability": [p[0] for p in preds],
    "post_alzheimer_diagnosis_probability":  [p[1] for p in preds],
    "pre_alzheimer_diagnosis_probability":  [p[2] for p in preds],
}

predictions_df = pd.DataFrame.from_dict(predictions)
predictions_df.head(3)
Out[13]:
row_id normal_diagnosis_probability post_alzheimer_diagnosis_probability pre_alzheimer_diagnosis_probability
0 LA9JQ1JZMJ9D2MBZV 0.958330 0.024677 0.016993
1 PSSRCWAPTAG72A1NT 0.841258 0.080386 0.078356
2 GCTODIZJB42VCBZRZ 0.998962 0.000609 0.000428

Save predictions 📨

In [14]:
predictions_df.to_csv(AICROWD_PREDICTIONS_PATH, index=False)

Submit to AIcrowd 🚀

NOTE: PLEASE SAVE THE NOTEBOOK BEFORE SUBMITTING IT (Ctrl + S)

In [15]:
!aicrowd login --api-key $AICROWD_API_KEY
!DATASET_PATH=$AICROWD_DATASET_PATH \
aicrowd notebook submit \
    --assets-dir $AICROWD_ASSETS_DIR \
    --challenge addi-alzheimers-detection-challenge
API Key valid
Saved API Key successfully!
Using notebook: /home/desktop0/FastAI Tabular Starter Notebook.ipynb for submission...
Removing existing files from submission directory...
Scrubbing API keys from the notebook...
Collecting notebook...
Validating the submission...
Executing install.ipynb...
[NbConvertApp] Converting notebook /home/desktop0/submission/install.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python
[NbConvertApp] Writing 1325 bytes to /home/desktop0/submission/install.nbconvert.ipynb
Executing predict.ipynb...
[NbConvertApp] Converting notebook /home/desktop0/submission/predict.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python
[NbConvertApp] Writing 19616 bytes to /home/desktop0/submission/predict.nbconvert.ipynb
submission.zip ━━━━━━━━━━━━━━━━━━━━━━ 100.0%1.4/1.4 MB856.0 kB/s0:00:00[0m • 0:00:01[36m0:00:01
                                                 ╭─────────────────────────╮                                                 
                                                 │ Successfully submitted! │                                                 
                                                 ╰─────────────────────────╯                                                 
                                                       Important links                                                       
┌──────────────────┬────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│  This submission │ https://www.aicrowd.com/challenges/addi-alzheimers-detection-challenge/submissions/134381              │
│                  │                                                                                                        │
│  All submissions │ https://www.aicrowd.com/challenges/addi-alzheimers-detection-challenge/submissions?my_submissions=true │
│                  │                                                                                                        │
│      Leaderboard │ https://www.aicrowd.com/challenges/addi-alzheimers-detection-challenge/leaderboards                    │
│                  │                                                                                                        │
│ Discussion forum │ https://discourse.aicrowd.com/c/addi-alzheimers-detection-challenge                                    │
│                  │                                                                                                        │
│   Challenge page │ https://www.aicrowd.com/challenges/addi-alzheimers-detection-challenge                                 │
└──────────────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────┘
In [ ]:


Comments

You must login before you can post a comment.

Execute