Kaggle competition: PetFinder.my Adoption Prediction
<SOS> metric: QuadraticWeightedKappa Kaggle data source: petfinder.my-adoption-prediction<predict_on_test>
%matplotlib inline
np.random.seed(seed=1337)
 warnings.filterwarnings('ignore')
 split_char = '/'
def predict(model, ckpts, dataloader):
predict_list=[]
with torch.no_grad() :
for _, data in enumerate(dataloader):
avg_preds = []
for ckpt in ckpts:
model.load_state_dict(ckpt['state_dict'])
 model.eval()
images, label = data.values()
images = images.to(device)
 outputs, _ = model(images)
 preds = F.softmax(outputs ).to('cpu' ).numpy()
avg_preds.append(preds)
 predict_list.append(np.mean(avg_preds, axis=0))
predict_list = np.concatenate(predict_list)
 return predict_list.argmax(axis=1 )<load_from_csv>
train = pd.read_csv('.. /input/petfinder-adoption-prediction/train/train.csv')
 test = pd.read_csv('.. /input/petfinder-adoption-prediction/test/test.csv')
 sample_submission = pd.read_csv('.. /input/petfinder-adoption-prediction/test/sample_submission.csv' )
test_df = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 test_dir = '.. /input/cassava-leaf-disease-classification/test_images/'
test_ds = CassavaDataset(dataframe=test_df,
root_dir=test_dir,
transforms=test_transforms())
 test_dl = DataLoader(test_ds, batch_size=bs,
shuffle=False, num_workers=8,
pin_memory=True )<load_pretrained>
import cv2
import os
from keras.applications.densenet import preprocess_input, DenseNet121
ckpts=[]
trained_model_path = ".. /input/cassavalblsmoothingresnet50"
for path in os.listdir(trained_model_path):
ckpts.append(torch.load(os.path.join(trained_model_path, path)))
<predict_on_test>
img_size = 256
batch_size = 256
test_predict_list=predict(model, ckpts, test_dl )<save_to_csv>
inp = Input(( 256,256,3))
backbone = DenseNet121(input_tensor = inp,
weights=".. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5",
include_top = False)
 x = backbone.output
x = GlobalAveragePooling2D()(x)
 x = Lambda(lambda x: K.expand_dims(x,axis = -1))(x)
 x = AveragePooling1D(4 )(x)
 out = Lambda(lambda x: x[:,:,0] )(x)
 m = Model(inp,out )
test_df['label'] = test_predict_list
test_df[['image_id', 'label']].to_csv('submission.csv', index=False)
 test_df.head()<import_modules>
pet_ids = train['PetID'].values
n_batches = len(pet_ids)// batch_size + 1
features = {}
for b in tqdm(range(n_batches)) :
start = b*batch_size
end =(b+1)*batch_size
batch_pets = pet_ids[start:end]
batch_images = np.zeros(( len(batch_pets),img_size,img_size,3))
for i,pet_id in enumerate(batch_pets):
try:
batch_images[i] = load_image(".. /input/petfinder-adoption-prediction/train_images/", pet_id)
 except:
pass
batch_preds = m.predict(batch_images)
 for i,pet_id in enumerate(batch_pets):
features[pet_id] = batch_preds[i]
import os
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.image import load_img,img_to_array,smart_resize
import matplotlib.pyplot as plt
import cv2
import pandas as pd
import json
import numpy as np 
<load_pretrained>
train_feats = pd.DataFrame.from_dict(features, orient='index')
 train_feats.columns = [f'pic_{i}' for i in range(train_feats.shape[1])]
model1=keras.models.load_model('.. /input/notebook841c84bbfb/IncepResNetV2.h5')
 model2=keras.models.load_model('.. /input/notebook841c84bbfb/EfficientNetB_V2.h5' )<load_from_csv>
pet_ids = test['PetID'].values
n_batches = len(pet_ids)// batch_size + 1
features = {}
for b in tqdm(range(n_batches)) :
start = b*batch_size
end =(b+1)*batch_size
batch_pets = pet_ids[start:end]
batch_images = np.zeros(( len(batch_pets),img_size,img_size,3))
for i,pet_id in enumerate(batch_pets):
try:
batch_images[i] = load_image(".. /input/petfinder-adoption-prediction/test_images/", pet_id)
 except:
pass
batch_preds = m.predict(batch_images)
 for i,pet_id in enumerate(batch_pets):
features[pet_id] = batch_preds[i]
train_dir='.. /input/cassava-leaf-disease-classification/train_images'
test_dir='.. /input/cassava-leaf-disease-classification/test_images'
train=pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 sample_sub=pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 sample_sub<prepare_x_and_y>
test_feats = pd.DataFrame.from_dict(features, orient='index')
 test_feats.columns = [f'pic_{i}' for i in range(test_feats.shape[1])]
def sample_df(sample_size=100):
df= train.sample(sample_size)
 df=df.reset_index()
return df
dfs=sample_df(sample_size=50)
 preds=[]
y_true=dfs['label']
for im_id in dfs.image_id:
img=load_img(train_dir + '/' + im_id)
 img=img_to_array(img)
 img=smart_resize(img,(512,512))
img=np.expand_dims(img,axis=0)
 img=img/255
pred=np.argmax(( 0.5*model1.predict(img)) +(0.5 * model2.predict(img)))
 preds.append(pred)
<prepare_output>
train_feats = train_feats.reset_index()
train_feats.rename({'index': 'PetID'}, axis='columns', inplace=True)
 test_feats = test_feats.reset_index()
test_feats.rename({'index': 'PetID'}, axis='columns', inplace=True )
sample_test=pd.DataFrame({'Prediction':preds, 'Actual':y_true})
 sample_test.head(30 )<predict_on_test>
all_ids = pd.concat([train, test], axis=0, ignore_index=True, sort=False)[['PetID']]
all_ids.shape
predictions=[]
for img_id in sample_sub.image_id:
img=load_img(test_dir + '/' + img_id)
 img=img_to_array(img)
 img=smart_resize(img,(512,512))
img=np.expand_dims(img,axis=0)
 img=img/255
lab=np.argmax(( model1.predict(img)* 0.5)+(model2.predict(img)*0.5))
predictions.append(lab)
 submission=pd.DataFrame({'image_id':sample_sub.image_id , 'label':predictions})
 print(submission)
 submission.to_csv('submission.csv',index=False )<define_variables>
n_components = 32
svd_ = TruncatedSVD(n_components=n_components, random_state=1337)
 features_df = pd.concat([train_feats, test_feats], axis=0)
 features = features_df[[f'pic_{i}' for i in range(256)]].values
svd_col = svd_.fit_transform(features)
 svd_col = pd.DataFrame(svd_col)
 svd_col = svd_col.add_prefix('IMG_SVD_')
 img_features = pd.concat([all_ids, svd_col], axis=1 )
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master' 
<import_modules>
labels_breed = pd.read_csv('.. /input/petfinder-adoption-prediction/breed_labels.csv')
 labels_state = pd.read_csv('.. /input/petfinder-adoption-prediction/color_labels.csv')
 labels_color = pd.read_csv('.. /input/petfinder-adoption-prediction/state_labels.csv' )
from glob import glob
from sklearn.model_selection import GroupKFold, StratifiedKFold
import cv2
from skimage import io
import torch
from torch import nn
import os
from datetime import datetime
import time
import random
import cv2
import torchvision
from torchvision import transforms
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
import sklearn
import warnings
import joblib
from sklearn.metrics import roc_auc_score, log_loss
from sklearn import metrics
import warnings
import cv2
import pydicom
import timm
from scipy.ndimage.interpolation import zoom
from sklearn.metrics import log_loss<init_hyperparams>
train_image_files = sorted(glob.glob('.. /input/petfinder-adoption-prediction/train_images/*.jpg'))
train_metadata_files = sorted(glob.glob('.. /input/petfinder-adoption-prediction/train_metadata/*.json'))
train_sentiment_files = sorted(glob.glob('.. /input/petfinder-adoption-prediction/train_sentiment/*.json'))
print(f'num of train images files: {len(train_image_files)}')
 print(f'num of train metadata files: {len(train_metadata_files)}')
 print(f'num of train sentiment files: {len(train_sentiment_files)}')
 test_image_files = sorted(glob.glob('.. /input/petfinder-adoption-prediction/test_images/*.jpg'))
test_metadata_files = sorted(glob.glob('.. /input/petfinder-adoption-prediction/test_metadata/*.json'))
test_sentiment_files = sorted(glob.glob('.. /input/petfinder-adoption-prediction/test_sentiment/*.json'))
print(f'num of test images files: {len(test_image_files)}')
 print(f'num of test metadata files: {len(test_metadata_files)}')
 print(f'num of test sentiment files: {len(test_sentiment_files)}' )
CFG = {
'fold_num': 5,
'seed': 719,
'model_arch': 'tf_efficientnet_b4_ns',
'img_size': 512,
'epochs': 10,
'train_bs': 32,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 3,
'used_epochs': [9,1,5],
'weights': [1,1,1]
}<load_from_csv>
class PetFinderParser(object):
def __init__(self, debug=False):
self.debug = debug
self.sentence_sep = ' '
self.extract_sentiment_text = False
def open_json_file(self, filename):
with open(filename, 'r', encoding='utf-8')as f:
json_file = json.load(f)
 return json_file
def parse_sentiment_file(self, file):

 file_sentiment = file['documentSentiment']
file_entities = [x['name'] for x in file['entities']]
file_entities = self.sentence_sep.join(file_entities)
 file_sentences_sentiment = [x['sentiment'] for x in file['sentences']]
file_sentences_sentiment = pd.DataFrame.from_dict(
 file_sentences_sentiment, orient='columns')
 file_sentences_sentiment_df = pd.DataFrame(
 {
'magnitude_sum': file_sentences_sentiment['magnitude'].sum(axis=0),
'score_sum': file_sentences_sentiment['score'].sum(axis=0),
'magnitude_mean': file_sentences_sentiment['magnitude'].mean(axis=0),
'score_mean': file_sentences_sentiment['score'].mean(axis=0),
'magnitude_var': file_sentences_sentiment['magnitude'].var(axis=0),
'score_var': file_sentences_sentiment['score'].var(axis=0),
}, index=[0] 
)
 df_sentiment = pd.DataFrame.from_dict(file_sentiment, orient='index' ).T
df_sentiment = pd.concat([df_sentiment, file_sentences_sentiment_df], axis=1)
 df_sentiment['entities'] = file_entities
df_sentiment = df_sentiment.add_prefix('sentiment_')
 return df_sentiment
def parse_metadata_file(self, file):

 file_keys = list(file.keys())
 if 'labelAnnotations' in file_keys:
file_annots = file['labelAnnotations']
file_top_score = np.asarray([x['score'] for x in file_annots] ).mean()
file_top_desc = [x['description'] for x in file_annots]
else:
file_top_score = np.nan
file_top_desc = ['']
file_colors = file['imagePropertiesAnnotation']['dominantColors']['colors']
file_crops = file['cropHintsAnnotation']['cropHints']
file_color_score = np.asarray([x['score'] for x in file_colors] ).mean()
file_color_pixelfrac = np.asarray([x['pixelFraction'] for x in file_colors] ).mean()
file_crop_conf = np.asarray([x['confidence'] for x in file_crops] ).mean()
if 'importanceFraction' in file_crops[0].keys() :
file_crop_importance = np.asarray([x['importanceFraction'] for x in file_crops] ).mean()
else:
file_crop_importance = np.nan
df_metadata = {
'annots_score': file_top_score,
'color_score': file_color_score,
'color_pixelfrac': file_color_pixelfrac,
'crop_conf': file_crop_conf,
'crop_importance': file_crop_importance,
'annots_top_desc': self.sentence_sep.join(file_top_desc)
 }
df_metadata = pd.DataFrame.from_dict(df_metadata, orient='index' ).T
df_metadata = df_metadata.add_prefix('metadata_')
 return df_metadata
def extract_additional_features(pet_id, mode='train'):
sentiment_filename = f'.. /input/petfinder-adoption-prediction/{mode}_sentiment/{pet_id}.json'
try:
sentiment_file = pet_parser.open_json_file(sentiment_filename)
 df_sentiment = pet_parser.parse_sentiment_file(sentiment_file)
 df_sentiment['PetID'] = pet_id
except FileNotFoundError:
df_sentiment = []
dfs_metadata = []
metadata_filenames = sorted(glob.glob(f'.. /input/petfinder-adoption-prediction/{mode}_metadata/{pet_id}*.json'))
if len(metadata_filenames)> 0:
for f in metadata_filenames:
metadata_file = pet_parser.open_json_file(f)
 df_metadata = pet_parser.parse_metadata_file(metadata_file)
 df_metadata['PetID'] = pet_id
dfs_metadata.append(df_metadata)
 dfs_metadata = pd.concat(dfs_metadata, ignore_index=True, sort=False)
 dfs = [df_sentiment, dfs_metadata]
return dfs
pet_parser = PetFinderParser()
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head()<count_values>
aggregates = ['sum', 'mean', 'var']
sent_agg = ['sum']
train_metadata_desc = train_dfs_metadata.groupby(['PetID'])['metadata_annots_top_desc'].unique()
train_metadata_desc = train_metadata_desc.reset_index()
train_metadata_desc[
'metadata_annots_top_desc'] = train_metadata_desc[
'metadata_annots_top_desc'].apply(lambda x: ' '.join(x))
prefix = 'metadata'
train_metadata_gr = train_dfs_metadata.drop(['metadata_annots_top_desc'], axis=1)
 for i in train_metadata_gr.columns:
if 'PetID' not in i:
train_metadata_gr[i] = train_metadata_gr[i].astype(float)
 train_metadata_gr = train_metadata_gr.groupby(['PetID'] ).agg(aggregates)
 train_metadata_gr.columns = pd.Index([f'{c[0]}_{c[1].upper() }' for c in train_metadata_gr.columns.tolist() ])
 train_metadata_gr = train_metadata_gr.reset_index()
train_sentiment_desc = train_dfs_sentiment.groupby(['PetID'])['sentiment_entities'].unique()
train_sentiment_desc = train_sentiment_desc.reset_index()
train_sentiment_desc[
'sentiment_entities'] = train_sentiment_desc[
'sentiment_entities'].apply(lambda x: ' '.join(x))
prefix = 'sentiment'
train_sentiment_gr = train_dfs_sentiment.drop(['sentiment_entities'], axis=1)
 for i in train_sentiment_gr.columns:
if 'PetID' not in i:
train_sentiment_gr[i] = train_sentiment_gr[i].astype(float)
 train_sentiment_gr = train_sentiment_gr.groupby(['PetID'] ).agg(sent_agg)
 train_sentiment_gr.columns = pd.Index([f'{c[0]}' for c in train_sentiment_gr.columns.tolist() ])
 train_sentiment_gr = train_sentiment_gr.reset_index()
test_metadata_desc = test_dfs_metadata.groupby(['PetID'])['metadata_annots_top_desc'].unique()
test_metadata_desc = test_metadata_desc.reset_index()
test_metadata_desc[
'metadata_annots_top_desc'] = test_metadata_desc[
'metadata_annots_top_desc'].apply(lambda x: ' '.join(x))
prefix = 'metadata'
test_metadata_gr = test_dfs_metadata.drop(['metadata_annots_top_desc'], axis=1)
 for i in test_metadata_gr.columns:
if 'PetID' not in i:
test_metadata_gr[i] = test_metadata_gr[i].astype(float)
 test_metadata_gr = test_metadata_gr.groupby(['PetID'] ).agg(aggregates)
 test_metadata_gr.columns = pd.Index([f'{c[0]}_{c[1].upper() }' for c in test_metadata_gr.columns.tolist() ])
 test_metadata_gr = test_metadata_gr.reset_index()
test_sentiment_desc = test_dfs_sentiment.groupby(['PetID'])['sentiment_entities'].unique()
test_sentiment_desc = test_sentiment_desc.reset_index()
test_sentiment_desc[
'sentiment_entities'] = test_sentiment_desc[
'sentiment_entities'].apply(lambda x: ' '.join(x))
prefix = 'sentiment'
test_sentiment_gr = test_dfs_sentiment.drop(['sentiment_entities'], axis=1)
 for i in test_sentiment_gr.columns:
if 'PetID' not in i:
test_sentiment_gr[i] = test_sentiment_gr[i].astype(float)
 test_sentiment_gr = test_sentiment_gr.groupby(['PetID'] ).agg(sent_agg)
 test_sentiment_gr.columns = pd.Index([f'{c[0]}' for c in test_sentiment_gr.columns.tolist() ])
 test_sentiment_gr = test_sentiment_gr.reset_index()
train.label.value_counts()<load_from_csv>
train_proc = train.copy()
train_proc = train_proc.merge(
 train_sentiment_gr, how='left', on='PetID')
 train_proc = train_proc.merge(
 train_metadata_gr, how='left', on='PetID')
 train_proc = train_proc.merge(
 train_metadata_desc, how='left', on='PetID')
 train_proc = train_proc.merge(
 train_sentiment_desc, how='left', on='PetID')
 test_proc = test.copy()
test_proc = test_proc.merge(
 test_sentiment_gr, how='left', on='PetID')
 test_proc = test_proc.merge(
 test_metadata_gr, how='left', on='PetID')
 test_proc = test_proc.merge(
 test_metadata_desc, how='left', on='PetID')
 test_proc = test_proc.merge(
 test_sentiment_desc, how='left', on='PetID')
 print(train_proc.shape, test_proc.shape)
 assert train_proc.shape[0] == train.shape[0]
assert test_proc.shape[0] == test.shape[0]
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()<categorify>
train_breed_main = train_proc[['Breed1']].merge(
 labels_breed, how='left',
left_on='Breed1', right_on='BreedID',
suffixes=('', '_main_breed'))
train_breed_main = train_breed_main.iloc[:, 2:]
train_breed_main = train_breed_main.add_prefix('main_breed_')
 train_breed_second = train_proc[['Breed2']].merge(
 labels_breed, how='left',
left_on='Breed2', right_on='BreedID',
suffixes=('', '_second_breed'))
train_breed_second = train_breed_second.iloc[:, 2:]
train_breed_second = train_breed_second.add_prefix('second_breed_')
 train_proc = pd.concat(
 [train_proc, train_breed_main, train_breed_second], axis=1)
 test_breed_main = test_proc[['Breed1']].merge(
 labels_breed, how='left',
left_on='Breed1', right_on='BreedID',
suffixes=('', '_main_breed'))
test_breed_main = test_breed_main.iloc[:, 2:]
test_breed_main = test_breed_main.add_prefix('main_breed_')
 test_breed_second = test_proc[['Breed2']].merge(
 labels_breed, how='left',
left_on='Breed2', right_on='BreedID',
suffixes=('', '_second_breed'))
test_breed_second = test_breed_second.iloc[:, 2:]
test_breed_second = test_breed_second.add_prefix('second_breed_')
 test_proc = pd.concat(
 [test_proc, test_breed_main, test_breed_second], axis=1)
 print(train_proc.shape, test_proc.shape )
class CassavaDataset(Dataset):
def __init__(
 self, df, data_root, transforms=None, output_label=True 
):
super().__init__()
self.df = df.reset_index(drop=True ).copy()
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index: int):
if self.output_label:
target = self.df.iloc[index]['label']
path = "{}/{}".format(self.data_root, self.df.iloc[index]['image_id'])
 img = get_img(path)
 if self.transforms:
img = self.transforms(image=img)['image']
if self.output_label == True:
return img, target
else:
return img<normalization>
X = pd.concat([train_proc, test_proc], ignore_index=True, sort=False )
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_valid_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightenessContrast(brightness_limit=(-0.1, 0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)
def get_inference_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.) 
<choose_model_class>
X_temp = X.copy()
text_columns = ['Description', 'metadata_annots_top_desc', 'sentiment_entities']
categorical_columns = ['main_breed_BreedName', 'second_breed_BreedName']
to_drop_columns = ['PetID', 'Name', 'RescuerID']
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x<create_dataframe>
X_temp['name_length'] = X['Name'].apply(lambda x: len(x)if type(x)is str else 0)
test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms() , output_label=False)
 tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False,)
 device = torch.device(CFG['device'])
 model = CassvaImgClassifier(CFG['model_arch'], 5 ).to(device )<load_pretrained>
rescuer_count = X.groupby(['RescuerID'])['PetID'].count().reset_index()
rescuer_count.columns = ['RescuerID', 'RescuerID_COUNT']
X_temp = X_temp.merge(rescuer_count, how='left', on='RescuerID' )
tst_preds = []
model_name = ['net1_fold0_epoch0','tf_efficientnet_b4_ns_fold_0_4','tf_efficientnet_b4_ns_fold_3_5']
for i in range(len(model_name)) :
model.load_state_dict(torch.load('.. /input/model903/{}'.format(model_name[i])))
 with torch.no_grad() :
for _ in range(CFG['tta']):
tst_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
tst_preds = np.mean(tst_preds, axis=0)
 del model
torch.cuda.empty_cache()<feature_engineering>
for i in categorical_columns:
X_temp.loc[:, i] = pd.factorize(X_temp.loc[:, i])[0]
test['label'] = np.argmax(tst_preds, axis=1)
 test.head()<feature_engineering>
X_text = X_temp[text_columns]
for i in X_text.columns:
X_text.loc[:, i] = X_text.loc[:, i].fillna('none' )
test['label'] = np.argmax(tst_preds, axis=1)
 test.head()<save_to_csv>
X_temp['Length_Description'] = X_text['Description'].map(len)
 X_temp['Length_metadata_annots_top_desc'] = X_text['metadata_annots_top_desc'].map(len)
 X_temp['Lengths_sentiment_entities'] = X_text['sentiment_entities'].map(len )
test.to_csv('submission.csv', index=False )<set_options>
corpus = [nltk.word_tokenize(text)for text in tqdm(X_tempy['Description'].fillna('<MISSING>' ).values)]
def word2vec_agg(description):
vectors = Magnitude(".. /input/magnitude-embeddings/glove.6B.300d.magnitude")
 return(np.mean(vectors.query(description),axis=0))
descriptions = Parallel(n_jobs=6, verbose=1 )(
 delayed(word2vec_agg )(description)for description in corpus)
 n_components=10
svd_ = TruncatedSVD(n_components=n_components, random_state=1337)
 word2vec_cols = svd_.fit_transform(descriptions)
 word2vec_cols = pd.DataFrame(word2vec_cols)
 word2vec_cols = word2vec_cols.add_prefix('SVD_{}_'.format("word2vecs"))
X_temp = pd.concat([X_temp, word2vec_cols], axis=1 )
%matplotlib inline<import_modules>
n_components = 16
text_features = []
for i in X_text.columns:
print(f'generating features from: {i}')
 tfv = TfidfVectorizer(min_df=2, max_features=None,
strip_accents='unicode', analyzer='word', token_pattern=r'(?u)\b\w+\b',
ngram_range=(1, 3), use_idf=1, smooth_idf=1, sublinear_tf=1)
 svd_ = TruncatedSVD(
 n_components=n_components, random_state=1337)
 tfidf_col = tfv.fit_transform(X_text.loc[:, i].values)
 svd_col = svd_.fit_transform(tfidf_col)
 svd_col = pd.DataFrame(svd_col)
 svd_col = svd_col.add_prefix('TFIDF_{}_'.format(i))
text_features.append(svd_col)
 text_features = pd.concat(text_features, axis=1)
 X_temp = pd.concat([X_temp, text_features], axis=1)
 for i in X_text.columns:
X_temp = X_temp.drop(i, axis=1 )
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import CosineAnnealingLR, CosineAnnealingWarmRestarts
import torch.utils.data as data
from torch.utils.data import DataLoader
import torchvision
from torchvision import transforms
from torch.cuda.amp import autocast, GradScaler<install_modules>
X_temp = X_temp.merge(img_features, how='left', on='PetID' )
!pip install '.. /input/efficientnet-pytorch-07/efficientnet_pytorch-0.7.0' 
<feature_engineering>
train_df_ids = train[['PetID']]
test_df_ids = test[['PetID']]
train_df_imgs = pd.DataFrame(train_image_files)
 train_df_imgs.columns = ['image_filename']
train_imgs_pets = train_df_imgs['image_filename'].apply(lambda x: x.split(split_char)[-1].split('-')[0])
 test_df_imgs = pd.DataFrame(test_image_files)
 test_df_imgs.columns = ['image_filename']
test_imgs_pets = test_df_imgs['image_filename'].apply(lambda x: x.split(split_char)[-1].split('-')[0])
 train_df_imgs = train_df_imgs.assign(PetID=train_imgs_pets)
 test_df_imgs = test_df_imgs.assign(PetID=test_imgs_pets)
 def getSize(filename):
st = os.stat(filename)
 return st.st_size
def getDimensions(filename):
img_size = Image.open(filename ).size
return img_size
train_df_imgs['image_size'] = train_df_imgs['image_filename'].apply(getSize)
 train_df_imgs['temp_size'] = train_df_imgs['image_filename'].apply(getDimensions)
 train_df_imgs['width'] = train_df_imgs['temp_size'].apply(lambda x : x[0])
 train_df_imgs['height'] = train_df_imgs['temp_size'].apply(lambda x : x[1])
 train_df_imgs = train_df_imgs.drop(['temp_size'], axis=1)
 test_df_imgs['image_size'] = test_df_imgs['image_filename'].apply(getSize)
 test_df_imgs['temp_size'] = test_df_imgs['image_filename'].apply(getDimensions)
 test_df_imgs['width'] = test_df_imgs['temp_size'].apply(lambda x : x[0])
 test_df_imgs['height'] = test_df_imgs['temp_size'].apply(lambda x : x[1])
 test_df_imgs = test_df_imgs.drop(['temp_size'], axis=1)
 aggs = {
'image_size': ['sum', 'mean', 'var'],
'width': ['sum', 'mean', 'var'],
'height': ['sum', 'mean', 'var'],
}
agg_train_imgs = train_df_imgs.groupby('PetID' ).agg(aggs)
 new_columns = [
k + '_' + agg for k in aggs.keys() for agg in aggs[k]
]
agg_train_imgs.columns = new_columns
agg_train_imgs = agg_train_imgs.reset_index()
agg_test_imgs = test_df_imgs.groupby('PetID' ).agg(aggs)
 new_columns = [
k + '_' + agg for k in aggs.keys() for agg in aggs[k]
]
agg_test_imgs.columns = new_columns
agg_test_imgs = agg_test_imgs.reset_index()
agg_imgs = pd.concat([agg_train_imgs, agg_test_imgs], axis=0 ).reset_index(drop=True )
DEBUG = False
INFERENCE = True
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 SEED = 87
seed_everything(SEED)
 TRAIN = '.. /input/cassava-leaf-disease-classification/train.csv'
TRAIN_IMGS = '.. /input/cassava-leaf-disease-classification/train_images'
TEST = '.. /input/cassava-leaf-disease-classification/sample_submission.csv'
TEST_IMGS = '.. /input/cassava-leaf-disease-classification/test_images'
cfg = {'model':{'arch': 'efficientnet-b0'},
'data':{'img_size': 224},
'optim':{'lr': 1e-3,
'T_0': 5,
'T_mult': 1,
'eta_min': 1e-6},
'train':{'batch_size': 64,
'shuffle': True,
'num_epochs': 5,
'patience': 2,
'use_checkpoint': False,
'checkpoint_path': '.. /input/cassava-checkpoints-effnet'},
'val':{'batch_size': 64,
'shuffle': False},
'test':{'batch_size': 64,
'shuffle': False,
'TTA': True,
'num_TTA': 5,
'checkpoint': '.. /input/cassava-checkpoints-effnet'}}<feature_engineering>
X_temp = X_temp.merge(agg_imgs, how='left', on='PetID' )
if INFERENCE:
test_df = pd.DataFrame()
test_df['image_id'] = list(os.listdir(TEST_IMGS + '/'))
test_df.loc[:, 'img_path'] = TEST_IMGS + '/' + test_df.image_id
if DEBUG:
test_df = pd.read_csv(TRAIN ).head(128)
 test_df.loc[:, 'img_path'] = TRAIN_IMGS + '/' + test_df.image_id
else:
train_df = pd.read_csv(TRAIN)
 train_df.loc[:, 'img_path'] = TRAIN_IMGS + '/' + train_df.image_id
if DEBUG:
train_df = train_df.head(200)
 idx_set = [idx for idx in range(len(train_df)) ]
train_idx, val_idx = train_test_split(idx_set, test_size=0.2)
 val_df = train_df.iloc[val_idx].reset_index(drop=True)
 train_df = train_df.iloc[train_idx].reset_index(drop=True)
 val_df['pred'] = np.nan<count_values>
X_temp = X_temp.drop(to_drop_columns, axis=1 )
if(DEBUG)&(not INFERENCE):
num_labels = len(train_df.loc[:, 'label'].unique())
 print('train ds', train_df.loc[:, 'label'].value_counts(normalize=True ).values)
 print('val ds', val_df.loc[:, 'label'].value_counts(normalize=True ).values )<normalization>
X_train_non_null = X_train.fillna(-1)
 X_test_non_null = X_test.fillna(-1 )
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 class AlbumTransform() :
def __init__(self, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), resize=cfg['data']['img_size']):
self.data_transform = {
'train': Compose([
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=mean, std=std, max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
RandomResizedCrop(resize, resize),
ToTensorV2(p=1.0),
], p=1.0),
'val': Compose([
CenterCrop(resize, resize),
Normalize(mean=mean, std=std, max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0)
 ], p=1.0),
'test': Compose([
CenterCrop(resize, resize),
Normalize(mean=mean, std=std, max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0)
 ], p=1.0),
'test_tta': Compose([
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=mean, std=std, max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
RandomResizedCrop(resize, resize),
ToTensorV2(p=1.0),
], p=1.0)
 }
def __call__(self, img, phase='train'):
return self.data_transform[phase](image=img)['image']<normalization>
X_train_non_null.isnull().any().any() , X_test_non_null.isnull().any().any()
class CassavaDataset(data.Dataset):
def __init__(self, df, transform=None, phase='train'):
self.transform = transform
self.phase= phase
self.df = df
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_path = self.df.iloc[index].loc['img_path']
img = cv2.imread(img_path)
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 img_transformed = self.transform(img, self.phase)
 if(self.phase=='train' or self.phase=='val'):
label = self.df.iloc[index].loc['label']
return img_transformed, label
else:
return img_transformed<create_dataframe>
def confusion_matrix(rater_a, rater_b, min_rating=None, max_rating=None):

 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(rater_a + rater_b)
 if max_rating is None:
max_rating = max(rater_a + rater_b)
 num_ratings = int(max_rating - min_rating + 1)
 conf_mat = [[0 for i in range(num_ratings)]
for j in range(num_ratings)]
for a, b in zip(rater_a, rater_b):
conf_mat[a - min_rating][b - min_rating] += 1
return conf_mat
def histogram(ratings, min_rating=None, max_rating=None):

 if min_rating is None:
min_rating = min(ratings)
 if max_rating is None:
max_rating = max(ratings)
 num_ratings = int(max_rating - min_rating + 1)
 hist_ratings = [0 for x in range(num_ratings)]
for r in ratings:
hist_ratings[r - min_rating] += 1
return hist_ratings
def quadratic_weighted_kappa(y, y_pred):

 rater_a = y
rater_b = y_pred
min_rating=None
max_rating=None
rater_a = np.array(rater_a, dtype=int)
 rater_b = np.array(rater_b, dtype=int)
 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(min(rater_a), min(rater_b))
if max_rating is None:
max_rating = max(max(rater_a), max(rater_b))
conf_mat = confusion_matrix(rater_a, rater_b,
min_rating, max_rating)
 num_ratings = len(conf_mat)
 num_scored_items = float(len(rater_a))
hist_rater_a = histogram(rater_a, min_rating, max_rating)
 hist_rater_b = histogram(rater_b, min_rating, max_rating)
 numerator = 0.0
denominator = 0.0
for i in range(num_ratings):
for j in range(num_ratings):
expected_count =(hist_rater_a[i] * hist_rater_b[j]
/ num_scored_items)
 d = pow(i - j, 2.0)/ pow(num_ratings - 1, 2.0)
 numerator += d * conf_mat[i][j] / num_scored_items
denominator += d * expected_count / num_scored_items
return(1.0 - numerator / denominator )
transform = AlbumTransform()
if INFERENCE:
if cfg['test']['TTA']:
test_ds = CassavaDataset(test_df, transform=transform, phase='test_tta')
 else:
test_ds = CassavaDataset(test_df, transform=transform, phase='test')
 test_dl = DataLoader(test_ds, batch_size=cfg['test']['batch_size'],
shuffle=cfg['test']['shuffle'], num_workers=os.cpu_count() , pin_memory=True)
 dataloaders = {'test': test_dl}
else:
train_ds = CassavaDataset(train_df, transform=transform, phase='train')
 val_ds = CassavaDataset(val_df, transform=transform, phase='val')
 train_dl = DataLoader(train_ds, batch_size=cfg['train']['batch_size'],
shuffle=cfg['train']['shuffle'], num_workers=os.cpu_count() , pin_memory=True)
 val_dl = DataLoader(val_ds, batch_size=cfg['val']['batch_size'],
shuffle=cfg['val']['shuffle'], num_workers=os.cpu_count() , pin_memory=True)
 dataloaders = {'train': train_dl, 'val': val_dl}
if(DEBUG)&(not INFERENCE):
iterator = iter(train_dl)
 inputs, labels = next(iterator)
 print('input size: ', inputs.size())
 print('label size: ', labels.size() )<choose_model_class>
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
preds = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 return -cohen_kappa_score(y, preds, weights='quadratic')
 def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X = X, y = y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
preds = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 return preds
def coefficients(self):
return self.coef_['x']
class CassavaNet(nn.Module):
def __init__(self):
super().__init__()
if INFERENCE:
self.model = EfficientNet.from_name(cfg['model']['arch'], in_channels=3)
 else:
self.model = EfficientNet.from_pretrained(cfg['model']['arch'], in_channels=3)
 num_in_features = self.model._fc.in_features
self.model._fc = nn.Linear(num_in_features, 5)
 def forward(self, x):
x = self.model(x)
 return x<choose_model_class>
xgb_params = {
'eval_metric': 'rmse',
'seed': 1337,
'eta': 0.0123,
'subsample': 0.8,
'colsample_bytree': 0.85,
'tree_method': 'gpu_hist',
'device': 'gpu',
'silent': 1,
}
model = CassavaNet()<choose_model_class>
def run_xgb(params, X_train, X_test):
n_splits = 10
verbose_eval = 1000
num_rounds = 60000
early_stop = 500
kf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=1337)
 oof_train = np.zeros(( X_train.shape[0]))
oof_test = np.zeros(( X_test.shape[0], n_splits))
i = 0
for train_idx, valid_idx in kf.split(X_train, X_train['AdoptionSpeed'].values):
X_tr = X_train.iloc[train_idx, :]
X_val = X_train.iloc[valid_idx, :]
y_tr = X_tr['AdoptionSpeed'].values
X_tr = X_tr.drop(['AdoptionSpeed'], axis=1)
 y_val = X_val['AdoptionSpeed'].values
X_val = X_val.drop(['AdoptionSpeed'], axis=1)
 d_train = xgb.DMatrix(data=X_tr, label=y_tr, feature_names=X_tr.columns)
 d_valid = xgb.DMatrix(data=X_val, label=y_val, feature_names=X_val.columns)
 watchlist = [(d_train, 'train'),(d_valid, 'valid')]
model = xgb.train(dtrain=d_train, num_boost_round=num_rounds, evals=watchlist,
early_stopping_rounds=early_stop, verbose_eval=verbose_eval, params=params)
 valid_pred = model.predict(xgb.DMatrix(X_val, feature_names=X_val.columns), ntree_limit=model.best_ntree_limit)
 test_pred = model.predict(xgb.DMatrix(X_test, feature_names=X_test.columns), ntree_limit=model.best_ntree_limit)
 oof_train[valid_idx] = valid_pred
oof_test[:, i] = test_pred
i += 1
return model, oof_train, oof_test
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(params = model.parameters() , lr=cfg['optim']['lr'] )<train_model>
model, oof_train, oof_test = run_xgb(xgb_params, X_train_non_null, X_test_non_null )
class RunOneEpoch() :
def __init__(self, model, dataloaders, criterion, optimizer, scheduler=None):
self.model = model
self.dataloaders = dataloaders
self.criterion = criterion.to(device)
 self.optimizer = optimizer
self.scheduler = scheduler
def train(self, phase='train'):
batch_loss = 0.0
batch_corrects = 0
epoch_loss = 0.0
epoch_corrects = 0
if phase=='train':
model.train()
else:
model.eval()
model.to(device)
 scaler = GradScaler()
dl = self.dataloaders[phase]
preds_list = np.array([])
 progress_bar = tqdm(dl)
 for i,(inputs, labels)in enumerate(progress_bar):
inputs = inputs.to(device, non_blocking=True)
 labels = labels.to(device, non_blocking=True)
 optimizer.zero_grad()
with torch.set_grad_enabled(phase=='train'):
with autocast() :
outputs = self.model(inputs)
 loss = self.criterion.forward(outputs, labels)
 _, preds = torch.max(outputs, axis=1)
 if phase=='train':
scaler.scale(loss ).backward()
scaler.step(self.optimizer)
 scaler.update()
else:
preds_list = np.append(preds_list, preds.detach().cpu().numpy())
 batch_loss = loss.item() * cfg[phase]['batch_size']
batch_corrects = torch.sum(preds==labels.detach())
 batch_corrects = batch_corrects.detach().cpu().numpy()
batch_acc = batch_corrects / cfg[phase]['batch_size']
num_batch =(i + 1)* cfg[phase]['batch_size']
epoch_loss += batch_loss
epoch_corrects += batch_corrects
train_loss = epoch_loss / num_batch
train_acc = epoch_corrects / num_batch
batch_loss = loss.item()
progress_bar.set_description(f"{phase}　| Batch: loss: {round(batch_loss, 4)} acc: {round(batch_acc, 4)}, Epoch: loss: {round(train_loss, 4)} acc: {round(train_acc, 4)}")
 if phase=='train':
if self.scheduler: self.scheduler.step()
return train_loss, train_acc
else:
return train_loss, train_acc, preds_list
def inference(self, phase='test'):
model.eval()
model.to(device)
 dl = self.dataloaders[phase]
preds_list = np.array([])
 progress_bar = tqdm(dl)
 for i, inputs in enumerate(progress_bar):
inputs = inputs.to(device, non_blocking=True)
 optimizer.zero_grad()
with torch.set_grad_enabled(phase=='train'):
outputs = self.model(inputs)
 _, preds = torch.max(outputs, axis=1)
 preds_list = np.append(preds_list, preds.cpu().numpy())
 return preds_list
def inference_TTA(self, phase='test'):
model.eval()
dl = self.dataloaders[phase]
preds_list = np.array([])
 outputs_list = np.zeros(( len(dl.dataset), 5))
progress_bar = tqdm(dl)
 for tta in range(cfg['test']['num_TTA']):
outputs_tta = np.array([])
 for i, inputs in enumerate(progress_bar):
inputs = inputs.to(device, non_blocking=True)
 with torch.set_grad_enabled(phase=='train'):
outputs = self.model(inputs)
 outputs_tta = np.append(outputs_tta, outputs.detach().cpu().numpy())
 outputs_tta = outputs_tta.reshape(-1, 5)
 outputs_list = outputs_list + outputs_tta
outputs_list = torch.Tensor(outputs_list)
 _, preds_list = torch.max(outputs_list, axis=1)
 return preds_list, outputs_list<load_pretrained>
optR = OptimizedRounder()
optR.fit(oof_train, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(oof_train, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK = ", qwk )
def get_checkpoint(folder, checkpoint_name):
checkpoint_path = os.path.join(folder, checkpoint_name)
 checkpoint = torch.load(checkpoint_path, map_location=device)
 return checkpoint<choose_model_class>
coefficients_ = coefficients.copy()
train_predictions = optR.predict(oof_train, coefficients_ ).astype(np.int8)
 print(f'train pred distribution: {Counter(train_predictions)}')
 test_predictions = optR.predict(oof_test.mean(axis=1), coefficients_ ).astype(np.int8)
 print(f'test pred distribution: {Counter(test_predictions)}' )
%%time
if INFERENCE:
if cfg['test']['TTA']:
outputs = []
model = CassavaNet().to(device)
 try:
checkpoint = get_checkpoint(cfg['test']['checkpoint'], f'checkpoint.pth')
 model.load_state_dict(checkpoint['model'])
 except:
print('No checkpoint.')
 criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(params = model.parameters() , lr=cfg['optim']['lr'])
 run = RunOneEpoch(model=model, dataloaders=dataloaders,
criterion=criterion, optimizer=optimizer)
 _, outputs = run.inference_TTA()
_, preds = torch.max(outputs, axis=1)
 test_df.loc[:, 'label'] = preds
test_df.loc[:, 'label'] = test_df.loc[:, 'label'].astype('uint8')
 test_df.drop('img_path', inplace=True, axis=1)
 test_df.to_csv('submission.csv', index=False)
 else:
preds = run.inference()
test_df.loc[:, 'label'] = preds
test_df.loc[:, 'label'] = test_df.loc[:, 'label'].astype('uint8')
 test_df.drop('img_path', inplace=True, axis=1)
 test_df.to_csv('submission.csv', index=False)
 else:
if cfg['train']['use_checkpoint']:
checkpoint = get_checkpoint(cfg['train']['checkpoint_path'], f'checkpoint.pth')
 model.load_state_dict(checkpoint['model'])
 optimizer.load_state_dict(checkpoint['optimizer'])
 optimizer.param_groups[0]['lr'] = cfg['optim']['lr']
print('Loaded checkpoint state.')
 scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=cfg['optim']['T_0'],
T_mult=cfg['optim']['T_mult'],
eta_min=cfg['optim']['eta_min'])
 run = RunOneEpoch(model=model, dataloaders=dataloaders,
criterion=criterion, optimizer=optimizer, scheduler=scheduler)
 loss_train = []; acc_train = []
loss_val = []; acc_val = []
patience = cfg['train']['patience']; num_patience = cfg['train']['patience']
preds_oof = []
checkpoints = []
for i in range(cfg['train']['num_epochs']):
print(f'--------- Epoch {i}')
 loss, acc = run.train(phase='train')
 loss_train.append(loss); acc_train.append(acc)
 loss, acc, preds = run.train(phase='val')
 loss_val.append(loss); acc_val.append(acc)
 preds_oof.append(preds)
 save_list = {'epoch': i,
'model': run.model.state_dict() ,
'optimizer': run.optimizer.state_dict()
}
checkpoints.append(save_list)
 if i == 0:pass
else:
if loss > loss_val[-2]:
patience -= 1
if patience == 0:
print(f'Early stopping at epoch {i}.Checkpoint is at epoch {i-num_patience}')
 try:
loss_val_series = pd.Series(loss_val)
 idxmin_loss = loss_val_series.idxmin()
val_df.loc[:, 'pred'] = preds_oof[idxmin_loss]
torch.save(checkpoints[idxmin_loss], f'checkpoint.pth')
 except:
val_df.loc[:, 'pred'] = preds_oof[-1]
torch.save(checkpoints[-1], f'checkpoint.pth')
 break
else:
patience = cfg['train']['patience']
if i==cfg['train']['num_epochs']-1:
print('Reached maximum train epoch.')
 val_df.loc[:, 'pred'] = preds_oof[-1]
torch.save(checkpoints[-1], f'checkpoint.pth')
 fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 6))
ax[0].plot(loss_train, label='train', marker='o')
 ax[1].plot(acc_train, label='train', marker='o')
 ax[0].plot(loss_val, label='val', marker='o')
 ax[1].plot(acc_val, label='val', marker='o')
 ax[0].set_xlabel('Epoch'); ax[1].set_xlabel('Epoch')
 ax[0].set_ylabel('CE Loss'); ax[1].set_ylabel('Accuracy')
 ax[0].set_title(f'Cross Entropy Loss'); ax[1].set_title('Accuracy')
 ax[0].set_xlim(0,); ax[1].set_xlim(0,)
 ax[0].legend() ; ax[1].legend()
plt.show()
acc_idx = val_df.label==val_df.pred
acc = len(val_df.loc[acc_idx])/ len(val_df)
 print(f'OOF Accuracy: {round(acc, 5)}' )<import_modules>
Counter(train_predictions )
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
from tensorflow import keras<define_variables>
Counter(test_predictions )
test_batch_size = 8
image_size = 512<load_from_csv>
submission1 = pd.DataFrame({'PetID': test['PetID'].values, 'AdoptionSpeed': test_predictions} )
PATH = '.. /input/cassava-leaf-disease-classification/'
sub_df = pd.read_csv(PATH + 'sample_submission.csv' )<feature_engineering>
%matplotlib inline
np.random.seed(seed=1337)
 warnings.filterwarnings('ignore')
 split_char = '/'
os.listdir('.. /input' )
sub_df["path"] = PATH + 'test_images/' + sub_df["image_id"]<create_dataframe>
test_ds = tf.data.Dataset.from_tensor_slices(( sub_df.path.values, sub_df.label.values))<set_options>
AUTOTUNE = tf.data.experimental.AUTOTUNE<categorify>
def test_aug(image):
data = {"image":image}
aug_data = test_transforms(**data)
 aug_img = aug_data["image"]
aug_img = tf.cast(aug_img, tf.float32)
 return aug_img<data_type_conversions>
def process_data(image_path, label, func_aug):
image = tf.io.read_file(image_path)
 image = tf.image.decode_jpeg(image)
 image = tf.image.convert_image_dtype(image, tf.float32)
 aug_img = tf.numpy_function(func=func_aug, inp=[image], Tout=tf.float32)
 return aug_img, label<categorify>
test_ds_alb = test_ds.map(partial(process_data, func_aug=test_aug), num_parallel_calls=AUTOTUNE ).prefetch(AUTOTUNE )<categorify>
test_ds_alb = test_ds_alb.map(set_shapes, num_parallel_calls=AUTOTUNE ).batch(8 )<install_modules>
X_train = X_temp.loc[np.isfinite(X_temp.AdoptionSpeed), :]
X_test = X_temp.loc[~np.isfinite(X_temp.AdoptionSpeed), :]
X_test = X_test.drop(['AdoptionSpeed'], axis=1)
 assert X_train.shape[0] == train.shape[0]
assert X_test.shape[0] == test.shape[0]
train_cols = X_train.columns.tolist()
train_cols.remove('AdoptionSpeed')
 test_cols = X_test.columns.tolist()
assert np.all(train_cols == test_cols)
 X_train_non_null = X_train.fillna(-1)
 X_test_non_null = X_test.fillna(-1 )
os.system('pip install /kaggle/input/kerasapplications -q')
 os.system('pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps' )<choose_model_class>
def confusion_matrix(rater_a, rater_b, min_rating=None, max_rating=None):

 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(rater_a + rater_b)
 if max_rating is None:
max_rating = max(rater_a + rater_b)
 num_ratings = int(max_rating - min_rating + 1)
 conf_mat = [[0 for i in range(num_ratings)]
for j in range(num_ratings)]
for a, b in zip(rater_a, rater_b):
conf_mat[a - min_rating][b - min_rating] += 1
return conf_mat
def histogram(ratings, min_rating=None, max_rating=None):

 if min_rating is None:
min_rating = min(ratings)
 if max_rating is None:
max_rating = max(ratings)
 num_ratings = int(max_rating - min_rating + 1)
 hist_ratings = [0 for x in range(num_ratings)]
for r in ratings:
hist_ratings[r - min_rating] += 1
return hist_ratings
def quadratic_weighted_kappa(y, y_pred):

 rater_a = y
rater_b = y_pred
min_rating=None
max_rating=None
rater_a = np.array(rater_a, dtype=int)
 rater_b = np.array(rater_b, dtype=int)
 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(min(rater_a), min(rater_b))
if max_rating is None:
max_rating = max(max(rater_a), max(rater_b))
conf_mat = confusion_matrix(rater_a, rater_b,
min_rating, max_rating)
 num_ratings = len(conf_mat)
 num_scored_items = float(len(rater_a))
hist_rater_a = histogram(rater_a, min_rating, max_rating)
 hist_rater_b = histogram(rater_b, min_rating, max_rating)
 numerator = 0.0
denominator = 0.0
for i in range(num_ratings):
for j in range(num_ratings):
expected_count =(hist_rater_a[i] * hist_rater_b[j]
/ num_scored_items)
 d = pow(i - j, 2.0)/ pow(num_ratings - 1, 2.0)
 numerator += d * conf_mat[i][j] / num_scored_items
denominator += d * expected_count / num_scored_items
return(1.0 - numerator / denominator )
efficientnet = efn.EfficientNetB4(weights=None, include_top=False,
drop_connect_rate=0.3, input_shape=(image_size, image_size, 3))
inputs = Input(shape=(image_size, image_size, 3))
efficientnet = efficientnet(inputs)
 pooling = GlobalAveragePooling2D()(efficientnet)
 dropout = Dropout(0.3 )(pooling)
 outputs = Dense(5, activation="softmax" )(dropout)
 model = Model(inputs=inputs, outputs=outputs )<load_pretrained>
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
preds = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 return -cohen_kappa_score(y, preds, weights='quadratic')
 def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X = X, y = y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
preds = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 return preds
def coefficients(self):
return self.coef_['x']
preds = []
for fold in [2,3]:
model.load_weights('.. /input/effnetb45128weights/EffNetB4_512_8_weights_fold_{}.h5'.format(fold+1))
preds.append(model.predict(test_ds_alb, workers=4, verbose=1))<predict_on_test>
xgb_params = {
'eval_metric': 'rmse',
'seed': 1337,
'silent': 1,
}
def run_xgb(params, X_train, X_test):
n_splits = 5
verbose_eval = 1000
num_rounds = 30000
early_stop = 500
kf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=1337)
 oof_train = np.zeros(( X_train.shape[0]))
oof_test = np.zeros(( X_test.shape[0], n_splits))
i = 0
for train_idx, valid_idx in kf.split(X_train, X_train['AdoptionSpeed'].values):
X_tr = X_train.iloc[train_idx, :]
X_val = X_train.iloc[valid_idx, :]
y_tr = X_tr['AdoptionSpeed'].values
X_tr = X_tr.drop(['AdoptionSpeed'], axis=1)
 y_val = X_val['AdoptionSpeed'].values
X_val = X_val.drop(['AdoptionSpeed'], axis=1)
 d_train = xgb.DMatrix(data=X_tr, label=y_tr, feature_names=X_tr.columns)
 d_valid = xgb.DMatrix(data=X_val, label=y_val, feature_names=X_val.columns)
 watchlist = [(d_train, 'train'),(d_valid, 'valid')]
model = xgb.train(dtrain=d_train, num_boost_round=num_rounds, evals=watchlist,
early_stopping_rounds=early_stop, verbose_eval=verbose_eval, params=params)
 valid_pred = model.predict(xgb.DMatrix(X_val, feature_names=X_val.columns), ntree_limit=model.best_ntree_limit)
 test_pred = model.predict(xgb.DMatrix(X_test, feature_names=X_test.columns), ntree_limit=model.best_ntree_limit)
 oof_train[valid_idx] = valid_pred
oof_test[:, i] = test_pred
i += 1
return model, oof_train, oof_test
model, oof_train, oof_test = run_xgb(xgb_params, X_train_non_null, X_test_non_null )
preds = []
for fold in [2,3]:
model.load_weights('.. /input/effnetb45128weights/EffNetB4_512_8_weights_fold_{}.h5'.format(fold+1))
fold_preds = []
for i in range(5):
fold_preds.append(model.predict(test_ds_alb, workers=4, verbose=1))
preds.append(np.mean(fold_preds, axis=0))<prepare_output>
optR = OptimizedRounder()
optR.fit(oof_train, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(oof_train, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK = ", qwk )
preds_avg = np.mean(preds, axis=0 )<prepare_output>
coefficients_ = coefficients.copy()
train_predictions = optR.predict(oof_train, coefficients_ ).astype(np.int8)
 test_predictions = optR.predict(oof_test.mean(axis=1), coefficients_ ).astype(np.int8)
 submission2 = pd.DataFrame({'PetID': test['PetID'].values, 'AdoptionSpeed': test_predictions} )
y_pred = np.argmax(preds_avg, axis=-1 )<prepare_output>
train_df = pd.read_csv('.. /input/petfinder-adoption-prediction/train/train.csv')
 img_size = 256
batch_size = 16
sub_df['label'] = y_pred<drop_column>
pet_ids = train_df['PetID'].values
n_batches = len(pet_ids)// batch_size + 1
sub_df.drop(['path'], axis=1, inplace=True )<save_to_csv>
inp = Input(( 256,256,3))
backbone = DenseNet121(input_tensor = inp,
weights = ".. /input/densenet121weights/densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5",
include_top = False)
 x = backbone.output
x = GlobalAveragePooling2D()(x)
 x = Lambda(lambda x: K.expand_dims(x,axis = -1))(x)
 x = AveragePooling1D(4 )(x)
 out = Lambda(lambda x: x[:,:,0] )(x)
 m = Model(inp,out )
sub_df.to_csv('submission.csv', index=False )<install_modules>
features = {}
for b in tqdm_notebook(range(n_batches)) :
start = b*batch_size
end =(b+1)*batch_size
batch_pets = pet_ids[start:end]
batch_images = np.zeros(( len(batch_pets),img_size,img_size,3))
for i,pet_id in enumerate(batch_pets):
try:
batch_images[i] = load_image(".. /input/petfinder-adoption-prediction/train_images/", pet_id)
 except:
pass
batch_preds = m.predict(batch_images)
 for i,pet_id in enumerate(batch_pets):
features[pet_id] = batch_preds[i]
%%capture
!pip install.. /input/timm-pip/timm-0.3.1-py3-none-any.whl<import_modules>
train_feats = pd.DataFrame.from_dict(features, orient='index' )
tf.__version__<load_from_csv>
test_df = pd.read_csv('.. /input/petfinder-adoption-prediction/test/test.csv')
 pet_ids = test_df['PetID'].values
n_batches = len(pet_ids)// batch_size + 1
features = {}
for b in tqdm_notebook(range(n_batches)) :
start = b*batch_size
end =(b+1)*batch_size
batch_pets = pet_ids[start:end]
batch_images = np.zeros(( len(batch_pets),img_size,img_size,3))
for i,pet_id in enumerate(batch_pets):
try:
batch_images[i] = load_image(".. /input/petfinder-adoption-prediction/test_images/", pet_id)
 except:
pass
batch_preds = m.predict(batch_images)
 for i,pet_id in enumerate(batch_pets):
features[pet_id] = batch_preds[i]
test_feats = pd.DataFrame.from_dict(features, orient='index' )
train_df = pd.read_csv(dataset_path + "/" + 'train.csv')
 train_df.head()<load_from_csv>
train_feats.reset_index(inplace=True)
 test_feats.reset_index(inplace=True )
set_seed(999)
 dataset_path = Path('.. /input/cassava-leaf-disease-classification')
 os.listdir(dataset_path)
 BASE_DIR = ".. /input/cassava-leaf-disease-classification/"
train_df = pd.read_csv(".. /input/cassava-leaf-disease-classification/train.csv" )<define_variables>
def impact_coding(data, feature, target='y'):

 n_folds = 20
n_inner_folds = 10
impact_coded = pd.Series()
oof_default_mean = data[target].mean()
kf = KFold(n_splits=n_folds, shuffle=True)
 oof_mean_cv = pd.DataFrame()
split = 0
for infold, oof in kf.split(data[feature]):
impact_coded_cv = pd.Series()
kf_inner = KFold(n_splits=n_inner_folds, shuffle=True)
 inner_split = 0
inner_oof_mean_cv = pd.DataFrame()
oof_default_inner_mean = data.iloc[infold][target].mean()
for infold_inner, oof_inner in kf_inner.split(data.iloc[infold]):
oof_mean = data.iloc[infold_inner].groupby(by=feature)[target].mean()
impact_coded_cv = impact_coded_cv.append(data.iloc[infold].apply(
 lambda x: oof_mean[x[feature]]
if x[feature] in oof_mean.index
else oof_default_inner_mean
, axis=1))
inner_oof_mean_cv = inner_oof_mean_cv.join(pd.DataFrame(oof_mean), rsuffix=inner_split, how='outer')
 inner_oof_mean_cv.fillna(value=oof_default_inner_mean, inplace=True)
 inner_split += 1
oof_mean_cv = oof_mean_cv.join(pd.DataFrame(inner_oof_mean_cv), rsuffix=split, how='outer')
 oof_mean_cv.fillna(value=oof_default_mean, inplace=True)
 split += 1
impact_coded = impact_coded.append(data.iloc[oof].apply(
 lambda x: inner_oof_mean_cv.loc[x[feature]].mean()
if x[feature] in inner_oof_mean_cv.index
else oof_default_mean
, axis=1))
return impact_coded, oof_mean_cv.mean(axis=1), oof_default_mean
def frequency_encoding(df, col_name):
new_name = "{}_counts".format(col_name)
 new_col_name = "{}_freq".format(col_name)
 grouped = df.groupby(col_name ).size().reset_index(name=new_name)
 df = df.merge(grouped, how = "left", on = col_name)
 df[new_col_name] = df[new_name]/df[new_name].count()
del df[new_name]
return df
def confusion_matrix(rater_a, rater_b, min_rating=None, max_rating=None):

 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(rater_a + rater_b)
 if max_rating is None:
max_rating = max(rater_a + rater_b)
 num_ratings = int(max_rating - min_rating + 1)
 conf_mat = [[0 for i in range(num_ratings)]
for j in range(num_ratings)]
for a, b in zip(rater_a, rater_b):
conf_mat[a - min_rating][b - min_rating] += 1
return conf_mat
def histogram(ratings, min_rating=None, max_rating=None):

 if min_rating is None:
min_rating = min(ratings)
 if max_rating is None:
max_rating = max(ratings)
 num_ratings = int(max_rating - min_rating + 1)
 hist_ratings = [0 for x in range(num_ratings)]
for r in ratings:
hist_ratings[r - min_rating] += 1
return hist_ratings
def quadratic_weighted_kappa(y, y_pred):

 rater_a = y
rater_b = y_pred
min_rating=None
max_rating=None
rater_a = np.array(rater_a, dtype=int)
 rater_b = np.array(rater_b, dtype=int)
 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(min(rater_a), min(rater_b))
if max_rating is None:
max_rating = max(max(rater_a), max(rater_b))
conf_mat = confusion_matrix(rater_a, rater_b,
min_rating, max_rating)
 num_ratings = len(conf_mat)
 num_scored_items = float(len(rater_a))
hist_rater_a = histogram(rater_a, min_rating, max_rating)
 hist_rater_b = histogram(rater_b, min_rating, max_rating)
 numerator = 0.0
denominator = 0.0
for i in range(num_ratings):
for j in range(num_ratings):
expected_count =(hist_rater_a[i] * hist_rater_b[j]
/ num_scored_items)
 d = pow(i - j, 2.0)/ pow(num_ratings - 1, 2.0)
 numerator += d * conf_mat[i][j] / num_scored_items
denominator += d * expected_count / num_scored_items
return(1.0 - numerator / denominator)
 class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = quadratic_weighted_kappa(y, X_p)
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
def rmse(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))
def train_lightgbm(X_train, X_test, params, n_splits, num_rounds, verbose_eval, early_stop):
kfold = StratifiedKFold(n_splits=n_splits, random_state=1337)
 oof_train = np.zeros(( X_train.shape[0]))
oof_test = np.zeros(( X_test.shape[0], n_splits))
i = 0
for train_index, valid_index in kfold.split(X_train, X_train['AdoptionSpeed'].values):
X_tr = X_train.iloc[train_index, :]
X_val = X_train.iloc[valid_index, :]
y_tr = X_tr['AdoptionSpeed'].values
X_tr = X_tr.drop(['AdoptionSpeed'], axis=1)
 y_val = X_val['AdoptionSpeed'].values
X_val = X_val.drop(['AdoptionSpeed'], axis=1)
 print('
y_tr distribution: {}'.format(Counter(y_tr)))
 d_train = lgb.Dataset(X_tr, label=y_tr)
 d_valid = lgb.Dataset(X_val, label=y_val)
 watchlist = [d_train, d_valid]
print('training LGB:')
 model = lgb.train(params,
train_set=d_train,
num_boost_round=num_rounds,
valid_sets=watchlist,
verbose_eval=verbose_eval,
early_stopping_rounds=early_stop)
 val_pred = model.predict(X_val, num_iteration=model.best_iteration)
 test_pred = model.predict(X_test, num_iteration=model.best_iteration)
 oof_train[valid_index] = val_pred
oof_test[:, i] = test_pred
i += 1
return oof_train, oof_test
pet_parser = PetFinderParser()
def main() :
X_train = X_temp.loc[np.isfinite(X_temp.AdoptionSpeed), :]
X_test = X_temp.loc[~np.isfinite(X_temp.AdoptionSpeed), :]
X_test = X_test.drop(['AdoptionSpeed'], axis=1)
 print('X_train shape: {}'.format(X_train.shape))
print('X_test shape: {}'.format(X_test.shape))
assert X_train.shape[0] == train.shape[0]
assert X_test.shape[0] == test.shape[0]
train_cols = X_train.columns.tolist()
train_cols.remove('AdoptionSpeed')
 test_cols = X_test.columns.tolist()
np.random.seed(13)
 categorical_features = ["Type", "Breed1", "Breed2", "Color1" ,"Color2", "Color3", "State"]
impact_coding_map = {}
for f in categorical_features:
print("Impact coding for {}".format(f))
X_train["impact_encoded_{}".format(f)], impact_coding_mapping, default_coding = impact_coding(X_train, f, target="AdoptionSpeed")
 impact_coding_map[f] =(impact_coding_mapping, default_coding)
 mapping, default_mean = impact_coding_map[f]
X_test["impact_encoded_{}".format(f)] = X_test.apply(lambda x: mapping[x[f]] if x[f] in mapping
else default_mean, axis=1)
 for cat in categorical_features:
X_train = frequency_encoding(X_train, cat)
 X_test = frequency_encoding(X_test, cat)
 params = {'application': 'regression',
'boosting': 'gbdt',
'metric': 'rmse',
'num_leaves': 70,
'max_depth': 9,
'learning_rate': 0.01,
'bagging_fraction': 0.85,
'feature_fraction': 0.8,
'min_split_gain': 0.02,
'min_child_samples': 150,
'min_child_weight': 0.02,
'lambda_l2': 0.0475,
'verbosity': -1,
'data_random_seed': 17}
early_stop = 500
verbose_eval = 100
num_rounds = 10000
n_splits = 5
oof_train, oof_test = train_lightgbm(X_train, X_test, params, n_splits, num_rounds, verbose_eval, early_stop)
 optR = OptimizedRounder()
optR.fit(oof_train, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
pred_test_y_k = optR.predict(oof_train, coefficients)
 print("
Valid Counts = ", Counter(X_train['AdoptionSpeed'].values))
print("Predicted Counts = ", Counter(pred_test_y_k))
print("Coefficients = ", coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, pred_test_y_k)
 print("QWK = ", qwk)
 coefficients_ = coefficients.copy()
train_predictions = optR.predict(oof_train, coefficients_ ).astype(int)
 print('train pred distribution: {}'.format(Counter(train_predictions)))
 test_predictions = optR.predict(oof_test.mean(axis=1), coefficients_)
 print('test pred distribution: {}'.format(Counter(test_predictions)))
 submission = pd.DataFrame({'PetID': test['PetID'].values, 'AdoptionSpeed': test_predictions.astype(np.int32)})
 return submission
submission3 = main()
len_df = len(train_df)
 print(f"There are {len_df} images" )<feature_engineering>
submission1["AdoptionSpeed"] = submission1.AdoptionSpeed*0.4 + submission2.AdoptionSpeed*0.2 + submission3.AdoptionSpeed*0.4
submission1["AdoptionSpeed"] = submission1["AdoptionSpeed"].round().astype(int)
 submission1.to_csv('submission.csv', index=False )