Solution for Stage Prediction

Solution for Stage Prediction

In [1]:
```!git clone https://github.com/derInformatiker/AIcrowd-AIBlitz7-Solution.git
!pip install -r AIcrowd-AIBlitz7-Solution/challenge4/requirements.txt
!pip install aicrowd-cli==0.1
```
```Cloning into 'AIcrowd-AIBlitz7-Solution'...
remote: warning: multi-pack bitmap is missing required reverse index
remote: Enumerating objects: 317, done.
remote: Counting objects: 100% (317/317), done.
remote: Compressing objects: 100% (191/191), done.
remote: Total 317 (delta 186), reused 219 (delta 121), pack-reused 0
Receiving objects: 100% (317/317), 469.43 KiB | 18.78 MiB/s, done.
Resolving deltas: 100% (186/186), done.
Collecting pandas==1.0.5
|████████████████████████████████| 10.1MB 17.8MB/s
Collecting opencv-python==4.2.0.32
|████████████████████████████████| 28.2MB 112kB/s
Collecting pytorch-lightning==1.2.4
|████████████████████████████████| 829kB 45.9MB/s
Collecting efficientnet-pytorch==0.6.3
Collecting albumentations==0.4.6
|████████████████████████████████| 122kB 51.3MB/s
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas==1.0.5->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 1)) (2018.9)
Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.7/dist-packages (from pandas==1.0.5->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 1)) (1.19.5)
Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.7/dist-packages (from pandas==1.0.5->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 1)) (2.8.1)
Collecting future>=0.17.1
|████████████████████████████████| 829kB 46.9MB/s
Requirement already satisfied: tqdm>=4.41.0 in /usr/local/lib/python3.7/dist-packages (from pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (4.41.1)
Requirement already satisfied: torch>=1.4 in /usr/local/lib/python3.7/dist-packages (from pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (1.8.1+cu101)
Requirement already satisfied: tensorboard>=2.2.0 in /usr/local/lib/python3.7/dist-packages (from pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (2.4.1)
Collecting fsspec[http]>=0.8.1
|████████████████████████████████| 112kB 53.3MB/s
Collecting PyYAML!=5.4.*,>=5.1
|████████████████████████████████| 276kB 61.4MB/s
Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (1.4.1)
Collecting imgaug>=0.4.0
|████████████████████████████████| 952kB 41.3MB/s
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.6.1->pandas==1.0.5->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 1)) (1.15.0)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch>=1.4->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (3.7.4.3)
Requirement already satisfied: wheel>=0.26; python_version >= "3" in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (0.36.2)
Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (1.28.0)
Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (1.0.1)
Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (54.2.0)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (2.23.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (3.3.4)
Requirement already satisfied: grpcio>=1.24.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (1.32.0)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (0.4.3)
Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (3.12.4)
Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (0.12.0)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (1.8.0)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.7/dist-packages (from fsspec[http]>=0.8.1->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (3.8.1)
Collecting aiohttp; extra == "http"
|████████████████████████████████| 1.3MB 31.7MB/s
Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (7.1.2)
Requirement already satisfied: imageio in /usr/local/lib/python3.7/dist-packages (from imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (2.4.1)
Requirement already satisfied: scikit-image>=0.14.2 in /usr/local/lib/python3.7/dist-packages (from imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (0.16.2)
Requirement already satisfied: Shapely in /usr/local/lib/python3.7/dist-packages (from imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (1.7.1)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (3.2.2)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (4.2.1)
Requirement already satisfied: rsa<5,>=3.1.4; python_version >= "3.6" in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (4.7.2)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (0.2.8)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (1.24.3)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (2020.12.5)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (1.3.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata; python_version < "3.8"->fsspec[http]>=0.8.1->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (3.4.1)
Collecting multidict<7.0,>=4.5
|████████████████████████████████| 143kB 47.8MB/s
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.7/dist-packages (from aiohttp; extra == "http"->fsspec[http]>=0.8.1->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (20.3.0)
Collecting async-timeout<4.0,>=3.0
Collecting yarl<2.0,>=1.0
|████████████████████████████████| 296kB 56.6MB/s
Requirement already satisfied: networkx>=2.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.14.2->imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (2.5)
Requirement already satisfied: PyWavelets>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image>=0.14.2->imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (1.1.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (1.3.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (0.10.0)
Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.7/dist-packages (from rsa<5,>=3.1.4; python_version >= "3.6"->google-auth<2,>=1.6.3->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.2.0->pytorch-lightning==1.2.4->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 3)) (3.1.0)
Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.7/dist-packages (from networkx>=2.0->scikit-image>=0.14.2->imgaug>=0.4.0->albumentations==0.4.6->-r AIcrowd-AIBlitz7-Solution/challenge3/requirements.txt (line 5)) (4.4.2)
Building wheels for collected packages: efficientnet-pytorch, albumentations, future, PyYAML
Building wheel for efficientnet-pytorch (setup.py) ... done
Created wheel for efficientnet-pytorch: filename=efficientnet_pytorch-0.6.3-cp37-none-any.whl size=12420 sha256=4de89acb270b36ed56e89785762ea65276d273741c3dd6763f010fc3dc2579d4
Building wheel for albumentations (setup.py) ... done
Created wheel for albumentations: filename=albumentations-0.4.6-cp37-none-any.whl size=65163 sha256=c8893ed254994c9115053a76a6df54f57efaee1f21703563fd2c652513a23bec
Building wheel for future (setup.py) ... done
Created wheel for future: filename=future-0.18.2-cp37-none-any.whl size=491058 sha256=c0e67cb8a2c83dcce6a090a86d86dd80aeaa7595712da162af958dec6d995595
Stored in directory: /root/.cache/pip/wheels/8b/99/a0/81daf51dcd359a9377b110a8a886b3895921802d2fc1b2397e
Building wheel for PyYAML (setup.py) ... done
Created wheel for PyYAML: filename=PyYAML-5.3.1-cp37-cp37m-linux_x86_64.whl size=44620 sha256=48904c9ecdcb655aa9f619f04f0a087c58c021282a2130a425e748e55733f10e
Stored in directory: /root/.cache/pip/wheels/a7/c1/ea/cf5bd31012e735dc1dfea3131a2d5eae7978b251083d6247bd
Successfully built efficientnet-pytorch albumentations future PyYAML
ERROR: google-colab 1.0.0 has requirement pandas~=1.1.0; python_version >= "3.0", but you'll have pandas 1.0.5 which is incompatible.
Installing collected packages: pandas, opencv-python, future, multidict, async-timeout, yarl, aiohttp, fsspec, PyYAML, pytorch-lightning, efficientnet-pytorch, imgaug, albumentations
Found existing installation: pandas 1.1.5
Uninstalling pandas-1.1.5:
Successfully uninstalled pandas-1.1.5
Found existing installation: opencv-python 4.1.2.30
Uninstalling opencv-python-4.1.2.30:
Successfully uninstalled opencv-python-4.1.2.30
Found existing installation: future 0.16.0
Uninstalling future-0.16.0:
Successfully uninstalled future-0.16.0
Found existing installation: PyYAML 3.13
Uninstalling PyYAML-3.13:
Successfully uninstalled PyYAML-3.13
Found existing installation: imgaug 0.2.9
Uninstalling imgaug-0.2.9:
Successfully uninstalled imgaug-0.2.9
Found existing installation: albumentations 0.1.12
Uninstalling albumentations-0.1.12:
Successfully uninstalled albumentations-0.1.12
Successfully installed PyYAML-5.3.1 aiohttp-3.7.4.post0 albumentations-0.4.6 async-timeout-3.0.1 efficientnet-pytorch-0.6.3 fsspec-0.9.0 future-0.18.2 imgaug-0.4.0 multidict-5.1.0 opencv-python-4.2.0.32 pandas-1.0.5 pytorch-lightning-1.2.4 yarl-1.6.3
```
```Collecting aicrowd-cli==0.1
|████████████████████████████████| 40kB 2.3MB/s
Requirement already satisfied: click<8,>=7.1.2 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli==0.1) (7.1.2)
Collecting gitpython
|████████████████████████████████| 163kB 17.9MB/s
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli==0.1) (2.23.0)
Collecting requests-toolbelt
|████████████████████████████████| 61kB 8.5MB/s
Collecting rich
|████████████████████████████████| 204kB 38.8MB/s
Requirement already satisfied: toml in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli==0.1) (0.10.2)
Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli==0.1) (4.41.1)
Collecting gitdb<5,>=4.0.1
|████████████████████████████████| 71kB 10.0MB/s
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->aicrowd-cli==0.1) (1.24.3)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->aicrowd-cli==0.1) (2020.12.5)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->aicrowd-cli==0.1) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->aicrowd-cli==0.1) (2.10)
Collecting commonmark<0.10.0,>=0.9.0
|████████████████████████████████| 51kB 8.1MB/s
Requirement already satisfied: typing-extensions<4.0.0,>=3.7.4 in /usr/local/lib/python3.7/dist-packages (from rich->aicrowd-cli==0.1) (3.7.4.3)
Collecting colorama<0.5.0,>=0.4.0
Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /usr/local/lib/python3.7/dist-packages (from rich->aicrowd-cli==0.1) (2.6.1)
Collecting smmap<5,>=3.0.1
Building wheels for collected packages: aicrowd-cli
Building wheel for aicrowd-cli (setup.py) ... done
Created wheel for aicrowd-cli: filename=aicrowd_cli-0.1.0-cp37-none-any.whl size=34929 sha256=4b79e7fd91a34e7d84e022bb6056132d92548bb51cdbfd6b0552678eec3ba179
Successfully built aicrowd-cli
Installing collected packages: smmap, gitdb, gitpython, requests-toolbelt, commonmark, colorama, rich, aicrowd-cli
Successfully installed aicrowd-cli-0.1.0 colorama-0.4.4 commonmark-0.9.1 gitdb-4.0.7 gitpython-3.1.14 requests-toolbelt-0.9.1 rich-10.1.0 smmap-4.0.0
```

RESTART RUNTIME TO USE NEW PACKAGES¶

In [1]:
```API_KEY = ""  # Please enter your API Key from [https://www.aicrowd.com/participants/me]
```
```API Key valid
Saved API Key successfully!
```
In [14]:
```!aicrowd dataset download --challenge stage-prediction

!rm -rf data
!mkdir data

!unzip -q test.zip  -d data
import os
os.rename('data/test', 'data/train')
!mv sample_submission.csv data/sample_submission.csv
```
```sample_submission.csv: 100% 13.3k/13.3k [00:00<00:00, 796kB/s]
test.zip: 100% 27.7M/27.7M [00:02<00:00, 10.6MB/s]
```
In [15]:
```import shutil

shutil.copy('AIcrowd-AIBlitz7-Solution/challenge4/model.py','model.py')
shutil.copy('AIcrowd-AIBlitz7-Solution/challenge4/dataset.py','dataset.py')
```
Out[15]:
`'dataset.py'`
In [16]:
```import pandas as pd
baseline = []
images_list = [598,1048,1198,1498,1798]
last = 0
for u, i in enumerate(images_list):
baseline.extend([u]*(i-last))
last = i
baseline = [0]+baseline

df.label = baseline
df.to_csv('data/labels.csv',index = False)
```
In [17]:
```import model
import dataset
import torch
import albumentations as A
import pytorch_lightning as pl
from pytorch_lightning import Trainer

if __name__ == '__main__':
trainer = Trainer(max_epochs = 30,gpus = 1, precision=16, amp_level='O1',deterministic=True)

train_tr = A.Compose([
A.CenterCrop(256,256,always_apply=True)
])

val_tr = A.Compose([
A.CenterCrop(256,256,always_apply=True)
])

model = model.Classifier({'lr':3e-4,'batch_size':32,'train_tr':train_tr,'val_tr':val_tr})
trainer.fit(model)
trainer.test(model)
out = trainer.predict(model)
output = torch.tensor([])

for i in range(len(out)):
output = torch.cat((output,torch.tensor(out[i][0]).argmax(1)))
output = output.int().tolist()
dataset.writeSub(output)
```
```GPU available: True, used: True
TPU available: None, using: 0 TPU cores
Using native 16bit precision.

| Name   | Type         | Params
----------------------------------------
0 | resnet | EfficientNet | 10.7 M
----------------------------------------
10.7 M    Trainable params
0         Non-trainable params
10.7 M    Total params
42.816    Total estimated model params size (MB)
```
```Loaded pretrained weights for efficientnet-b3
```
```/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py:477: UserWarning: This DataLoader will create 6 worker processes in total. Our suggested max number of worker in current system is 2, which is smaller than what this DataLoader is going to create. Please be aware that excessive worker creation might get DataLoader running slow or even freeze, lower the worker number to avoid potential slowness/freeze if necessary.
cpuset_checked))
```
```/content/model.py:23: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
prob = F.softmax(x)
```

```/usr/local/lib/python3.7/dist-packages/pytorch_lightning/utilities/distributed.py:51: UserWarning: you defined a test_step but have no test_dataloader. Skipping test loop
warnings.warn(*args, **kwargs)
```

In [ ]:
```
```