Kaggle competition: PetFinder.my Adoption Prediction
with open(os.path.join(BASE_DIR, "label_num_to_disease_map.json")) as file:
map_classes = json.loads(file.read())
 map_classes = {int(k): v for k, v in map_classes.items() }
print(json.dumps(map_classes, indent=4))
train_df = pd.read_csv(os.path.join(BASE_DIR, "train.csv"))
train_df["class_name"] = train_df["label"].map(map_classes)
 train_df['path'] = train_df['image_id'].map(lambda x:dataset_path/'train_images'/x)
 train_df = train_df.drop(columns=['image_id'])
 train_df = train_df.sample(frac=1 ).reset_index(drop=True )<drop_column>
tqdm.pandas()
gc.collect()
np.random.seed(369 )
train_df = train_df[["label", "path"]]<train_model>
df_train = pd.read_csv('.. /input/train/train.csv')
 df_breed = pd.read_csv('.. /input/breed_labels.csv')
 df_color = pd.read_csv('.. /input/color_labels.csv')
 df_state = pd.read_csv('.. /input/state_labels.csv')
 df_test = pd.read_csv('.. /input/test/test.csv' )
im = Image.open(train_df['path'][1])
 width, height = im.size
print(width,height )<create_dataframe>
df_train.isna().sum()
dls = ImageDataLoaders.from_df(train_df,
valid_pct=0.2,
seed=999,
label_col=0,
fn_col=1,
bs=bs,
item_tfms=item_tfms,
batch_tfms=batch_tfms )<categorify>
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 )
def create_timm_body(arch:str, pretrained=True, cut=None, n_in=3):
"Creates a body from any model in the `timm` library."
model = create_model(arch, pretrained=pretrained, num_classes=0, global_pool='')
 _update_first_layer(model, n_in, pretrained)
 if cut is None:
ll = list(enumerate(model.children()))
cut = next(i for i,o in reversed(ll)if has_pool_type(o))
if isinstance(cut, int): return nn.Sequential(*list(model.children())[:cut])
 elif callable(cut): return cut(model)
 else: raise NamedError("cut must be either integer or function")
 def create_timm_model(arch:str, n_out, cut=None, pretrained=True, n_in=3, init=nn.init.kaiming_normal_, custom_head=None,
concat_pool=True, **kwargs):
"Create custom architecture using `arch`, `n_in` and `n_out` from the `timm` library"
body = create_timm_body(arch, pretrained, None, n_in)
 if custom_head is None:
nf = num_features_model(nn.Sequential(*body.children())) *(2 if concat_pool else 1)
 head = create_head(nf, n_out, concat_pool=concat_pool, **kwargs)
 else: head = custom_head
model = nn.Sequential(body, head)
 if init is not None: apply_init(model[1], init)
 return model
def timm_learner(dls, arch:str, loss_func=None, pretrained=True, cut=None, splitter=None,
y_range=None, config=None, n_out=None, normalize=True, **kwargs):
"Build a convnet style learner from `dls` and `arch` using the `timm` library"
if config is None: config = {}
if n_out is None: n_out = get_c(dls)
 assert n_out, "`n_out` is not defined, and could not be inferred from data, set `dls.c` or pass `n_out`"
if y_range is None and 'y_range' in config: y_range = config.pop('y_range')
 model = create_timm_model(arch, n_out, default_split, pretrained, y_range=y_range, **config)
 learn = Learner(dls, model, loss_func=loss_func, splitter=default_split, **kwargs)
 if pretrained: learn.freeze()
return learn<choose_model_class>
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']
learn = timm_learner(dls,
'tf_efficientnet_b0_ns',
opt_func = ranger,
loss_func=LabelSmoothingCrossEntropy() ,
metrics = [accuracy] ).to_native_fp16()<find_best_params>
def rmse(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))
learn.lr_find()<train_model>
train_desc = df_train.Description.fillna("none" ).values
test_desc = df_test.Description.fillna("none" ).values
svd_n_components = 200
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, 
)
 tfv.fit(list(train_desc))
X = tfv.transform(train_desc)
 X_test = tfv.transform(test_desc)
 svd = TruncatedSVD(n_components=svd_n_components)
 svd.fit(X)
 print(svd.explained_variance_ratio_.sum())
 print(svd.explained_variance_ratio_)
 X = svd.transform(X)
 X = pd.DataFrame(X, columns=['svd_{}'.format(i)for i in range(svd_n_components)])
 df_train = pd.concat(( df_train, X), axis=1)
 X_test = svd.transform(X_test)
 X_test = pd.DataFrame(X_test, columns=['svd_{}'.format(i)for i in range(svd_n_components)])
 df_test = pd.concat(( df_test, X_test), axis=1 )
learn.freeze()
learn.fit_flat_cos(1,1e-1, wd=0.5, cbs=[MixUp() ] )<save_model>
train_sentiment_path = '.. /input/train_sentiment/'
test_sentiment_path = '.. /input/test_sentiment/'
train_meta_path = '.. /input/train_metadata/'
test_meta_path = '.. /input/test_metadata/'
learn.save('stage-1' )<load_pretrained>
df_train[['desc_magnitude', 'desc_score']] = df_train['PetID'].progress_apply(lambda x: get_sentiment(x, train_sentiment_path))
df_test[['desc_magnitude', 'desc_score']] = df_test['PetID'].progress_apply(lambda x: get_sentiment(x, test_sentiment_path))
learn = learn.load('stage-1' )<find_best_params>
learn.unfreeze()
learn.lr_find()<train_model>
df_train.isna().sum()
learn.unfreeze()
learn.fit_flat_cos(20,2e-2,pct_start=0,cbs=[MixUp() ] )<categorify>
train_petid = df_train['PetID']
test_petid = df_test['PetID']
learn = learn.to_native_fp32()<save_model>
df_train.drop(['Name','Description', 'RescuerID', 'PetID'], axis=1, inplace=True)
 df_test.drop(['Name','Description', 'RescuerID', 'PetID'], axis=1, inplace=True )
learn.save('stage-2' )<choose_model_class>
magnitude_std = df_train.desc_magnitude.std()
magnitude_mean = df_train.desc_magnitude.mean()
score_std = df_train.desc_score.std()
score_mean = df_train.desc_score.mean()
df_train['desc_magnitude'].fillna(np.random.normal(magnitude_mean, magnitude_std), inplace=True)
 df_train['desc_score'].fillna(np.random.normal(score_mean, score_std), inplace=True)
 df_test['desc_magnitude'].fillna(np.random.normal(magnitude_mean, magnitude_std), inplace=True)
 df_test['desc_score'].fillna(np.random.normal(score_mean, score_std), inplace=True )
interp = ClassificationInterpretation.from_learner(learn )<load_from_csv>
sample_df = pd.read_csv(dataset_path/'sample_submission.csv')
 sample_df.head()<categorify>
_sample_df = sample_df.copy()
_sample_df['path'] = _sample_df['image_id'].map(lambda x:dataset_path/'test_images'/x)
 _sample_df = _sample_df.drop(columns=['image_id'])
 test_dl = dls.test_dl(_sample_df )<define_variables>
test_dl.show_batch()<normalization>
from sklearn.model_selection import train_test_split
preds, _ = learn.tta(dl=test_dl, n=8, beta=0 )<feature_engineering>
X = df_train.drop(['AdoptionSpeed'], axis=1)
 y = df_train.AdoptionSpeed
sample_df['label'] = preds.argmax(dim=-1 ).numpy()<save_to_csv>
X_train, X_val, y_train, y_val = train_test_split(X, y, train_size=0.8, random_state=1 )
sample_df.to_csv('submission.csv',index=False )<import_modules>
def train_cats(df):
for n,c in df.items() :
if is_string_dtype(c): df[n] = c.astype('category' ).cat.as_ordered()
def apply_cats(df, trn):
for n,c in df.items() :
if(n in trn.columns)and(trn[n].dtype.name=='category'):
df[n] = pd.Categorical(c, categories=trn[n].cat.categories, ordered=True)
import os
import random
import numpy as np
import pandas as pd
import cv2
import matplotlib.pyplot as plt
import torch
import torchvision
from torchvision import transforms
import tensorflow as tf
import albumentations as A
import imgaug.augmenters as iaa
from imgaug import parameters as iap<define_variables>
model_rf = RandomForestClassifier()
model_rf.fit(X_train,y_train)
 model_rf.score(X_train,y_train )
DIR = ".. /input/cassava-leaf-disease-classification/train_images"
image_path = f'{DIR}/100042118.jpg'<set_options>
model_rf.score(X_val,y_val )
torch_trans_list = [transforms.CenterCrop(( 178, 178)) ,
transforms.Resize(128),
transforms.RandomRotation(45),
transforms.RandomAffine(35),
transforms.RandomCrop(128),
transforms.RandomHorizontalFlip(p=1),
transforms.RandomPerspective(p=1),
transforms.RandomVerticalFlip(p=1)]<install_modules>
print(model_rf.predict(X_train))
!pip install --quiet /kaggle/input/kerasapplications
!pip install --quiet /kaggle/input/efficientnet-git<set_options>
def score(model,X,y):
optR = OptimizedRounder()
optR.fit(model.predict(X), y)
 coefficients = optR.coefficients()
pred_y_k = optR.predict(model.predict(X), coefficients)
 print("Valid Counts = ", Counter(y))
print("Predicted Counts = ", Counter(pred_y_k))
print("Coefficients = ", coefficients)
 qwk = quadratic_weighted_kappa(y, pred_y_k)
 print("QWK = ", qwk)
 print("RMSE = ",rmse(y, model.predict(X)))
 print("mean Accuracy: ", model.score(X,y))
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 0
seed_everything(seed)
 warnings.filterwarnings('ignore')
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print(f'Running on TPU {tpu.master() }')
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}')
 BATCH_SIZE = 16 * REPLICAS
HEIGHT = 512
WIDTH = 512
CHANNELS = 3
N_CLASSES = 5
TTA_STEPS = 8
def data_augment(image, label):
p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_1 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_2 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_3 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 if p_spatial >.75:
image = tf.image.transpose(image)
 if p_rotate >.75:
image = tf.image.rot90(image, k=3)
 elif p_rotate >.5:
image = tf.image.rot90(image, k=2)
 elif p_rotate >.25:
image = tf.image.rot90(image, k=1)
 if p_pixel_1 >=.4:
image = tf.image.random_saturation(image, lower=.7, upper=1.3)
 if p_pixel_2 >=.4:
image = tf.image.random_contrast(image, lower=.8, upper=1.2)
 if p_pixel_3 >=.4:
image = tf.image.random_brightness(image, max_delta=.1)
 if p_crop >.7:
if p_crop >.9:
image = tf.image.central_crop(image, central_fraction=.7)
 elif p_crop >.8:
image = tf.image.central_crop(image, central_fraction=.8)
 else:
image = tf.image.central_crop(image, central_fraction=.9)
 elif p_crop >.4:
crop_size = tf.random.uniform([], int(HEIGHT*.8), HEIGHT, dtype=tf.int32)
 image = tf.image.random_crop(image, size=[crop_size, crop_size, CHANNELS])
 return image, label
def get_name(file_path):
parts = tf.strings.split(file_path, os.path.sep)
 name = parts[-1]
return name
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
return image
def center_crop(image):
image = tf.reshape(image, [600, 800, CHANNELS])
 h, w = image.shape[0], image.shape[1]
if h > w:
image = tf.image.crop_to_bounding_box(image,(h - w)// 2, 0, w, w)
 else:
image = tf.image.crop_to_bounding_box(image, 0,(w - h)// 2, h, h)
 image = tf.image.resize(image, [HEIGHT, WIDTH])
 return image
def resize_image(image, label):
image = tf.image.resize(image, [HEIGHT, WIDTH])
 image = tf.reshape(image, [HEIGHT, WIDTH, CHANNELS])
 return image, label
def process_path(file_path):
name = get_name(file_path)
 img = tf.io.read_file(file_path)
 img = decode_image(img)
 return img, name
def get_dataset(files_path, shuffled=False, tta=False, extension='jpg'):
dataset = tf.data.Dataset.list_files(f'{files_path}*{extension}', shuffle=shuffled)
 dataset = dataset.map(process_path, num_parallel_calls=AUTO)
 if tta:
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.map(resize_image, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 database_base_path = '/kaggle/input/cassava-leaf-disease-classification/'
submission = pd.read_csv(f'{database_base_path}sample_submission.csv')
 display(submission.head())
 TEST_FILENAMES = tf.io.gfile.glob(f'{database_base_path}test_tfrecords/ld_test*.tfrec')
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print(f'GCS: test: {NUM_TEST_IMAGES}')
 model_path_list = glob.glob('.. /input/casava-efficientnet-tpu-weights/*.h5')
 model_path_list.sort()
print('Models to predict:')
 print(*model_path_list, sep='
')
 def model_fn(input_shape, N_CLASSES):
inputs = L.Input(shape=input_shape, name='input_image')
 base_model = efn.EfficientNetB4(input_tensor=inputs,
include_top=False,
weights=None,
pooling='avg')
 x = L.Dropout (.5 )(base_model.output)
 output = L.Dense(N_CLASSES, activation='softmax', name='output' )(x)
 model = Model(inputs=inputs, outputs=output)
 return model
with strategy.scope() :
model = model_fn(( None, None, CHANNELS), N_CLASSES)
 files_path = f'{database_base_path}test_images/'
test_size = len(os.listdir(files_path))
test_preds = np.zeros(( test_size, N_CLASSES))
for model_path in model_path_list:
print(model_path)
 K.clear_session()
model.load_weights(model_path)
 if TTA_STEPS > 0:
test_ds = get_dataset(files_path, tta=True ).repeat()
ct_steps = TTA_STEPS *(( test_size/BATCH_SIZE)+ 1)
 preds = model.predict(test_ds, steps=ct_steps, verbose=1)[:(test_size * TTA_STEPS)]
preds = np.mean(preds.reshape(test_size, TTA_STEPS, N_CLASSES, order='F'), axis=1)
 test_preds += preds / len(model_path_list)
 else:
test_ds = get_dataset(files_path, tta=False)
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model.predict(x_test)/ len(model_path_list)
 test_preds = np.argmax(test_preds, axis=-1)
 test_names_ds = get_dataset(files_path)
 image_names = [img_name.numpy().decode('utf-8')for img, img_name in iter(test_names_ds.unbatch())]
submission = pd.DataFrame({'image_id': image_names, 'label': test_preds})
 submission.to_csv('submission.csv', index=False )<define_variables>
print('Train score: ', score(model_rf,X_train,y_train))
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master' 
<import_modules>
print('valid scoe: ', score(model_rf,X_val,y_val))
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
from sklearn import svm
import pickle<init_hyperparams>
model_rf = RandomForestClassifier(n_estimators=20,n_jobs=-1)
 model_rf.fit(X_train,y_train )
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" if torch.cuda.is_available() else "cpu",
'tta': 1,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}<load_from_csv>
print('Train score: ', score(model_rf,X_train,y_train))
print('valid scoe: ', score(model_rf,X_val,y_val))
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head()<set_options>
model_rf = RandomForestClassifier(n_estimators=40,n_jobs=-1)
 model_rf.fit(X_train,y_train)
 print('Train score: ', score(model_rf,X_train,y_train))
print('valid scoe: ', score(model_rf,X_val,y_val))
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
def get_img(path):
im_bgr = cv2.imread(path)
 im_rgb = im_bgr[:, :, ::-1]
return im_rgb
img = get_img('.. /input/cassava-leaf-disease-classification/train_images/1000015157.jpg')
 plt.imshow(img)
 plt.show()<categorify>
model_rf = RandomForestClassifier(n_estimators=80,n_jobs=-1)
 model_rf.fit(X_train,y_train)
 print('Train score: ', score(model_rf,X_train,y_train))
print('valid scoe: ', score(model_rf,X_val,y_val))
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>
model_rf = RandomForestClassifier(n_estimators=100,n_jobs=-1)
 model_rf.fit(X_train,y_train)
 print('Train score: ', score(model_rf,X_train,y_train))
print('valid scoe: ', score(model_rf,X_val,y_val))
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_inference_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),
RandomBrightnessContrast(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_valid_transforms() :
return Compose([
CenterCrop(CFG['img_size'], CFG['img_size'], p=1.) ,
Resize(CFG['img_size'], CFG['img_size']),
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>
model_gb = GradientBoostingClassifier(n_estimators=100,)
 model_gb.fit(X_train,y_train)
 print('Train score: ', score(model_gb,X_train,y_train))
print('valid scoe: ', score(model_gb,X_val,y_val))
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 = nn.Sequential(*list(self.model.children())[:-1] )(x)
 return x<load_pretrained>
model_gb = GradientBoostingClassifier(n_estimators=150)
 model_gb.fit(X_train,y_train)
 print('Train score: ', score(model_gb,X_train,y_train))
print('valid scoe: ', score(model_gb,X_val,y_val))
do_val = False
seed_everything(CFG['seed'])
 folds = StratifiedKFold(n_splits=CFG['fold_num'] ).split(np.arange(train.shape[0]), train.label.values)
 clf = pickle.load(open('.. /input/pytorch-efficientnetb4-svm-top-train/SVM_model.pkl', 'rb'))
for fold,(trn_idx, val_idx)in enumerate(folds):
if fold > 0:
break
print('Inference fold {} started'.format(fold))
if do_val:
valid_ = train.loc[val_idx,:].reset_index(drop=True)
 valid_ds = CassavaDataset(valid_, '.. /input/cassava-leaf-disease-classification/train_images/', transforms=get_valid_transforms() , output_label=False)
 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_valid_transforms() , output_label=False)
 if do_val:
val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=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'], train.label.nunique() ).to(device)
 val_preds = []
tst_preds = []
for i, epoch in enumerate(CFG['used_epochs']):
model.load_state_dict(torch.load('.. /input/pytorch-efficientnet-baseline-train-amp-aug/{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch)))
 with torch.no_grad() :
for _ in range(CFG['tta']):
if do_val:
val_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, val_loader, device)]
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)
 fin_test = clf.predict(tst_preds)
 if do_val:
val_preds = np.mean(val_preds, axis=0)
 fin_val = clf.predict(val_preds)
 print('fold {} validation accuracy = {:.5f}'.format(fold,(valid_.label.values==fin_val ).mean()))
del model
torch.cuda.empty_cache()<feature_engineering>
model_gb = GradientBoostingClassifier(n_estimators=180)
 model_gb.fit(X,y)
 print('Train score: ', score(model_gb,X,y))
print('Train score: ', score(model_gb,X_train,y_train))
print('valid scoe: ', score(model_gb,X_val,y_val))
test['label'] = fin_test
test.head()<save_to_csv>
model_xgb = XGBClassifier(learning_rate=0.09,n_estimators=150,objective='multi:softprob',n_jobs=-1,eval_metric= 'mlogloss',gamma=0.2)
 model_xgb.fit(X_train, y_train)
 print('Train score: ', score(model_xgb,X_train,y_train))
print('valid score: ', score(model_xgb,X_val,y_val))
test.to_csv('submission.csv', index=False )<install_modules>
params_lgbm = {'num_leaves': 25,
'min_data_in_leaf': 90,
'objective':'multiclass',
'num_class': 5,
'max_depth': 9,
'learning_rate': 0.03,
"boosting": "gbdt",
"feature_fraction": 0.9980062052116254,
"bagging_freq": 1,
"bagging_fraction": 0.844212672233457,
"bagging_seed": 11,
"metric": 'multi_logloss',
"lambda_l1": 0.12757257166471625,
"random_state": 133,
"verbosity": -1
}
%%time
!cp.. /input/rapids/rapids.0.11.0 /opt/conda/envs/rapids.tar.gz
!cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz
sys.path = ["/opt/conda/envs/rapids/lib"] + ["/opt/conda/envs/rapids/lib/python3.6"] + ["/opt/conda/envs/rapids/lib/python3.6/site-packages"] + sys.path
!cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/<import_modules>
lgbm_train = lgbm.Dataset(X_train, y_train, categorical_feature=category_columns)
 lgbm_valid = lgbm.Dataset(X_val, y_val, categorical_feature=category_columns )
from cuml.linear_model import Ridge 
<load_from_csv>
model_lgbm = lgbm.train(params_lgbm, lgbm_train, 50000, valid_sets=[lgbm_valid], verbose_eval= 5000, categorical_feature=category_columns, early_stopping_rounds = 500 )
train = cudf.read_csv('.. /input/multi-cat-encodings/X_train_te.csv')
 test = cudf.read_csv('.. /input/multi-cat-encodings/X_test_te.csv')
 sample_submission = cudf.read_csv('.. /input/cat-in-the-dat-ii/sample_submission.csv' )<define_variables>
print(( np.argmax(model_lgbm.predict(X_train), axis=1)== y_train ).sum() / y_train.shape[0])
 print(( np.argmax(model_lgbm.predict(X_val), axis=1)== y_val ).sum() / y_val.shape[0] )
train_oof = cp.zeros(( train.shape[0],))
test_preds = 0
train_oof.shape<compute_train_metric>
val_lgbm = model_lgbm.predict(X_val)
 val_gb = model_gb.predict_log_proba(X_val)
 val_mixed =(val_gb + val_lgbm)/ 2 
(np.argmax(val_mixed, axis=1)== y_val ).sum() / y_val.shape[0]
<find_best_model_class><EOS> 
test_pred = model_xgb.predict(df_test)
 test_petid = pd.DataFrame(test_petid)
 submission = test_petid.join(pd.DataFrame(test_pred, columns=['AdoptionSpeed']))
submission.to_csv('submission.csv', index=False )