"""
<div style="margin-left:50px;margin-right:50px;">
<center><div style="background:url(https://www.teahub.io/photos/full/19-197024_backgrounds-medical-hd-best-free-medicine-wallpapers-medicine.jpg); border-radius:8px;padding:20px; color:white;"><h1 style="font-size: 40px;">DATA EXPLANATION VIDEO</h1></div><br>
    <p style="font-size:20px">Did I actually sit down to discuss this competetion with myself? You bet I did.<br><br><a hreaf="https://www.linkedin.com/posts/vyom-bhatia-40ba79181_computervision-activity-6747848578720419840-wMNc">Check it out here on LinkedIn.</a></p></div>
   
"""
"""
<div style="margin-left:50px;margin-right:50px;">
<center><div style="background:url(https://www.teahub.io/photos/full/19-197024_backgrounds-medical-hd-best-free-medicine-wallpapers-medicine.jpg); border-radius:8px;padding:20px; color:white;"><h1 style="font-size: 40px;">TIME TO CODE</h1></div><br>
<p style="font-size:20px;margin-left:50px;margin-right:50px;">The point is simple, we need a model to solve the problem, let's build one. <br>
We will be using the latest <a href="https://paperswithcode.com/paper/bottleneck-transformers-for-visual">Bottleneck Transformers</a>!<br><br>Also,<br><br>I have my semesters in 15 days and I haven't really studied anything, just busy building models ;) <br>I have no idea what I am going to do :D</p></center></div>
"""
"""
# Importing Libraries + Data
"""
"""
### Installing the Bottleneck Transformer:
"""
!pip install bottleneck-transformer-pytorch
"""
### Importing Stuff:
"""
import torch
from torch import nn
from torchvision.models import resnet50

from bottleneck_transformer_pytorch import BottleStack

from fastai.vision.all import *

import pandas as pd
"""
#### Reading the CSV with annotations.
"""
data = pd.read_csv("../input/ranzcr-clip-catheter-line-classification/train_annotations.csv")
data.head()
"""
# Forming the Dataloader
"""
"""
### Defining the functions to grab those files.
"""
path = "../input/ranzcr-clip-catheter-line-classification"

def get_x(r):
    return path + "/" + 'train' + "/" + r['StudyInstanceUID'] + ".jpg"

def get_y(r):
    return r['label']
"""
### Finally setting up the Dataloader for FastAI.
### Well below you can see we got a lot of hyperparmeter tuning. Hit me up on [LinkedIn](https://www.linkedin.com/in/vyom-bhatia-40ba79181/) on [Instagram](https://instagram.com/vyombhatia) if you got any questions.
"""
def getblock(size=256,batch=3, data_df=data):
    block = DataBlock(blocks=(ImageBlock, CategoryBlock),
                      splitter=RandomSplitter(seed=42),
                      get_x=get_x, # Using the function above.
                      get_y=get_y, # Using the function above.
                      item_tfms = RandomResizedCrop(256), # Resizing images to 400 pixels.
                      
                      # Data Augmentation time:
                      batch_tfms = [*aug_transforms(mult=1.0, do_flip=False,
                                                   flip_vert=False, max_rotate=0,
                                                   min_zoom=1.0, max_zoom=1.0,
                                                   pad_mode="zeros",
                                                   xtra_tfms=None,max_warp=0,
                                                   p_affine=0, p_lighting=0),
                                    Normalize.from_stats(*imagenet_stats)])
    
    return block.dataloaders(data_df, bs=batch)
                      
    
dataloader = getblock()
"""
# Visualizing
"""
"""
### Lets see how the data augmentations is doing:
"""
dataloader.show_batch()
"""
# Building the model
"""
"""
### Easy Peasy grab a resnet50 on top of the BotNet and train the model using transfer learning:
"""
layer = BottleStack(
    dim = 256,
    fmap_size = 64,        # set specifically for imagenet's 224 x 224
    dim_out = 2048,
    proj_factor = 4,
    downsample = True,
    heads = 4,
    dim_head = 128,
    rel_pos_emb = True,
    activation = nn.ReLU()
)

resnet = resnet50()

# model surgery

backbone = list(resnet.children())

model = nn.Sequential(
    *backbone[:5],
    layer,
    nn.AdaptiveAvgPool2d((1, 1)),
    nn.Flatten(1),
    nn.Linear(2048, 1000)
)
learn = Learner(dataloader, model, metrics = accuracy)
"""
<div style="margin-left:50px;margin-right:50px;">
<center><div style="background:url(https://www.teahub.io/photos/full/19-197024_backgrounds-medical-hd-best-free-medicine-wallpapers-medicine.jpg); border-radius:8px;padding:20px; color:white;"><h1 style="font-size: 40px;">REMINDER</h1></div><br>
<p style="font-size:20px;margin-left:50px;margin-right:50px;">If you are planning to fork this, upvote and link me! :D</p></center></div>
"""
"""
# Learning Rate Finder
"""
"""
### I absolutely love this function from the FastAI library. 
### Why? It finds you the best Learning Rate.
"""
learn.lr_find()
learn.fine_tune(2, 0.0006918309954926372)
# The above LR was suggested by the lr_find.
"""
### Yes Karen, I know that the accuracy sucks with BotNet and my current hyperparameters for data augmentation. But this was just a little experimenting with the BotNet.

### A little background about me?

### I am an 18-year-old business major, Fresher at Delhi University. Thank you. Love from India.
"""
"""
<center><p style="font-size:20px;margin-left:50px;margin-right:50px;">
☁☁🌰🌰🌰☁☁<br>
☁☁👂👀👂☁☁<br>
☁☁👍👃👍☁☁<br>
☁☁👍〰👍☁☁<br>
☁🔵🔵🔵🔵🔵☁<br>
🔵🔵🔴💛🔴🔵🔵<br>
🔵☁🔵⁣🔴🔵☁🔵<br>
🔵☁🔵🔵🔵☁🔵<br>
👇☁🔴🔴🔴☁👇<br>
☁☁🔵🔴🔵☁☁<br>
☁☁🔵☁🔵☁☁<br>
☁☁🔵☁🔵☁☁<br>
☁☁👢☁👢☁☁<br>

 Random Superman Image with Emojis</p></center>
"""