Kaggle competition: APTOS 2019 Blindness Detection
group3['avg_questions_seen'] = group3.avg_questions.cumsum()<groupby>
package_path = '.. /input/efficientnet/efficientnet-pytorch/EfficientNet-PyTorch/'
sys.path.append(package_path )
results_u_final = train.loc[train.content_type_id == False, ['user_id','answered_correctly']].groupby(['user_id'] ).agg(['mean'])
 results_u_final.columns = ['answered_correctly_user']
results_u2_final = train.loc[train.content_type_id == False, ['user_id','prior_question_had_explanation']].groupby(['user_id'] ).agg(['mean'])
 results_u2_final.columns = ['explanation_mean_user']<merge>
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import torch.nn.functional as F
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy
from sklearn.metrics import confusion_matrix
from torch.utils.data import random_split, Dataset
from torch.utils.data.sampler import SubsetRandomSampler
import os
from PIL import Image
import glob
import cv2
import gc
from tqdm import tqdm
import seaborn as sns
from sklearn.metrics import confusion_matrix, cohen_kappa_score
import pandas as pd
from sklearn.model_selection import StratifiedKFold
from tqdm import tqdm
from efficientnet_pytorch import EfficientNet
train = pd.merge(train, questions_df, left_on = 'content_id', right_on = 'question_id', how = 'left' )<groupby>
class APTOSDataset(Dataset):

 def __init__(self, csv_file, filetype, transform=None):
self.eye_frame = pd.read_csv(csv_file)
 self.filetype = filetype
self.transform = transform
def __len__(self):
return len(self.eye_frame)
 def __getitem__(self, idx):
if self.filetype == 'train':
img_name = os.path.join('.. /input/aptos2019-blindness-detection/train_images',
self.eye_frame.loc[idx,'id_code'] + '.png')
 image = Image.open(img_name)
 if self.transform:
image = self.transform(image)
 else:
image = transforms.ToTensor()(image)
 return image,self.eye_frame.diagnosis[idx]
else:
img_name = os.path.join('.. /input/aptos2019-blindness-detection/test_images',
self.eye_frame.loc[idx,'id_code'] + '.png')
 image = Image.open(img_name)
 if self.transform:
image = self.transform(image)
 else:
image = transforms.ToTensor()(image)
 return image, self.eye_frame.loc[idx,'id_code']
results_q_final = train.loc[train.content_type_id == False, ['question_id','answered_correctly']].groupby(['question_id'] ).agg(['mean'])
 results_q_final.columns = ['quest_pct']<groupby>
class KFoldDataset(Dataset):

 def __init__(self, csv_file, indices, filetype, transform=None):
self.eye_frame = pd.read_csv(csv_file ).iloc[indices].reset_index(drop=True)
 self.filetype = filetype
self.transform = transform
def __len__(self):
return len(self.eye_frame)
 def __getitem__(self, idx):
if self.filetype == 'train':
img_name = os.path.join('.. /input/aptos2019-blindness-detection/train_images',
self.eye_frame.loc[idx,'id_code'] + '.png')
 image = Image.open(img_name)
 if self.transform:
image = self.transform(image)
 else:
image = transforms.ToTensor()(image)
 return image,self.eye_frame.diagnosis[idx]
else:
img_name = os.path.join('.. /input/aptos2019-blindness-detection/test_images',
self.eye_frame.loc[idx,'id_code'] + '.png')
 image = Image.open(img_name)
 if self.transform:
image = self.transform(image)
 else:
image = transforms.ToTensor()(image)
 return image, self.eye_frame.loc[idx,'id_code']
results_q2_final = train.loc[train.content_type_id == False, ['question_id','part']].groupby(['question_id'] ).agg(['count'])
 results_q2_final.columns = ['count']<merge>
question2 = pd.merge(questions_df, results_q_final, left_on = 'question_id', right_on = 'question_id', how = 'left' )<merge>
question2 = pd.merge(question2, results_q2_final, left_on = 'question_id', right_on = 'question_id', how = 'left' )<feature_engineering>
question2.quest_pct = round(question2.quest_pct, 5 )<drop_column>
BATCH_SIZE = 28
train_pd = pd.read_csv(r"/kaggle/input/aptos2019-blindness-detection/train.csv")
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 NUM_EPOCHS = 5
N_SPLITS = 5
start_time = time.time()
splits = list(StratifiedKFold(n_splits=N_SPLITS, shuffle=True, random_state=1234 ).split(train_pd['id_code'], train_pd['diagnosis']))
test_pd = pd.read_csv(r"/kaggle/input/aptos2019-blindness-detection/test.csv")
 predictionsfinal = torch.zeros(( len(test_pd),5), dtype=torch.float32)
 test_dataset = APTOSDataset(csv_file='.. /input/aptos2019-blindness-detection/test.csv', filetype='test',transform=test_transform)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)
 for fold_num,(train_idx, valid_idx)in enumerate(splits):
print("
")
 print("--- Fold Number: {} ---".format(fold_num+1))
model = EfficientNet.from_name('efficientnet-b2')
 num_ftrs = model._fc.in_features
model._fc = nn.Linear(num_ftrs, 5)
 model.load_state_dict(torch.load("/kaggle/input/pretrained-cv-weights/valid_fold_"+str(fold_num+1)+".pth"))
model = model.to(device)
 kfold_test_predictions = torch.zeros(( len(test_pd)) ,5)
 model.eval()
with torch.no_grad() :
for i,(inputs, img_id)in enumerate(tqdm(test_loader)) :
inputs = inputs.to(device)
 outputs = model(inputs)
 kfold_test_predictions[i*BATCH_SIZE:(i+1)*BATCH_SIZE] = outputs
predictionsfinal +=(kfold_test_predictions/N_SPLITS)
 _, preds = torch.max(predictionsfinal, 1)
 final_predictions = pd.DataFrame(( [test_pd['id_code'].tolist() , preds.tolist() ])).transpose()
final_predictions.columns = ['id_code', 'diagnosis']
final_predictions_df = final_predictions.copy()
final_predictions.to_csv("submission.csv",index=False)
 print("Training Complete --- {} seconds ---".format(round(( time.time() - start_time),2)) )