Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<define_variables>
!pip install -U /kaggle/input/kerasapplications
input_size = 224<normalization>
!pip install -U /kaggle/input/efficientnet/efficientnet-master
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<choose_model_class>
!pip install -U /kaggle/input/tensorflowresnets/TensorFlow-ResNets
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )<load_pretrained>
def is_interactive() :
return 'runtime' in get_ipython().config.IPKernelApp.connection_file
IS_INTERACTIVE = is_interactive()
print(IS_INTERACTIVE )
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint<predict_on_test>
import pandas as pd, numpy as np
from kaggle_datasets import KaggleDatasets
import tensorflow as tf, re, math
import tensorflow.keras.backend as K
import efficientnet.tfkeras as efn
from sklearn.model_selection import KFold
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report, precision_recall_curve
from IPython.display import display
import gc
import cv2
from tf2_resnets import models
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5<predict_on_test>
def _bytes_feature(value):

 if isinstance(value, type(tf.constant(0))):
value = value.numpy()
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):

 return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _int64_feature(value):

 return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def serialize_example(image,image_name):
feature = {
'image': _bytes_feature(image),
'image_name': _bytes_feature(image_name),
}
example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
return example_proto.SerializeToString()
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )<define_variables>
RESNEXT_ID = 10
N_TFRECORDS = 20
IMAGE_HEIGHT = 600
IMAGE_WIDTH = 800
os.mkdir('test_tfrecords_600')
 test_df = pd.DataFrame(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'),
columns=['image_name'])
 test_df['tfr_group'] = test_df.index%N_TFRECORDS
speed_test = False<predict_on_test>
for tfr_group in range(N_TFRECORDS):
df = test_df[test_df.tfr_group==tfr_group]
if df.shape[0]>0:
tfr_filename = 'test_tfrecords_600/cassava_test{}-{}.tfrec'.format(tfr_group,df.shape[0])
 print("Writing",tfr_filename)
 with tf.io.TFRecordWriter(tfr_filename)as writer:
for index,row in tqdm(df.iterrows()):
image_name = row['image_name']
image_path = '.. /input/cassava-leaf-disease-classification/test_images/'+image_name
image = cv2.imread(image_path)
 image = cv2.resize(image,(IMAGE_WIDTH,IMAGE_HEIGHT))
image_shape = image.shape
image = cv2.imencode('.jpg', image,(cv2.IMWRITE_JPEG_QUALITY, 100)) [1].tostring()
image_name = str.encode(image_name)
 sample = serialize_example(image,image_name)
 writer.write(sample)
 image_shape
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )<predict_on_test>
N_TFRECORDS = 20
IMAGE_HEIGHT = 666
IMAGE_WIDTH = 500
os.mkdir('test_tfrecords_500')
 test_df = pd.DataFrame(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'),
columns=['image_name'])
 test_df['tfr_group'] = test_df.index%N_TFRECORDS
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
for tfr_group in range(N_TFRECORDS):
df = test_df[test_df.tfr_group==tfr_group]
if df.shape[0]>0:
tfr_filename = 'test_tfrecords_500/cassava_test{}-{}.tfrec'.format(tfr_group,df.shape[0])
 print("Writing",tfr_filename)
 with tf.io.TFRecordWriter(tfr_filename)as writer:
for index,row in tqdm(df.iterrows()):
image_name = row['image_name']
image_path = '.. /input/cassava-leaf-disease-classification/test_images/'+image_name
image = cv2.imread(image_path)
 image = cv2.resize(image,(IMAGE_WIDTH,IMAGE_HEIGHT))
image_shape = image.shape
image = cv2.imencode('.jpg', image,(cv2.IMWRITE_JPEG_QUALITY, 100)) [1].tostring()
image_name = str.encode(image_name)
 sample = serialize_example(image,image_name)
 writer.write(sample)
 image_shape
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )<set_options>
DEVICE = "GPU"
FOLDS = 5
FOLD_TO_RUN = [0,1,2,3,4]
BATCH_SIZE = 128
EPOCHS = 15
N_WORKERS = 4
%matplotlib inline 
<create_dataframe>
if DEVICE == "TPU":
print("connecting to TPU...")
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print("Could not connect to TPU")
 tpu = None
if tpu:
try:
print("initializing TPU...")
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("TPU initialized")
 except _:
print("failed to initialize TPU")
 else:
DEVICE = "GPU"
if DEVICE != "TPU":
print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
if DEVICE == "GPU":
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
 AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )
frames_per_vid = [17, 25, 30, 32, 35, 36, 38, 39, 40, 49, 56, 64, 72, 80, 81, 82, 100]
public_LB = [0.46788, 0.46776, 0.46611, 0.46542, 0.46643, 0.46484, 0.46444, 0.46603, 0.46635, 0.46620, 0.46481, 0.46441, 0.46559, 0.46518, 0.46453, 0.46482, 0.46495]
df_viz = pd.DataFrame({'frames_per_vid': frames_per_vid, 'public_LB':public_LB} )<define_variables>
GCS_PATH = '.'
files_test_600 = np.sort(np.array(tf.io.gfile.glob(GCS_PATH + '/test_tfrecords_600/*.tfrec')))
 files_test_500 = np.sort(np.array(tf.io.gfile.glob(GCS_PATH + '/test_tfrecords_500/*.tfrec')))
 print(files_test_600)
 print(files_test_500 )
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 frame_h = 5
frame_l = 5
len(test_videos )<import_modules>
ROT_ = 180.0
SHR_ = 2.0
HZOOM_ = 8.0
WZOOM_ = 8.0
HSHIFT_ = 8.0
WSHIFT_ = 8.0
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 def get_3x3_mat(lst):
return tf.reshape(tf.concat([lst],axis=0), [3,3])
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = get_3x3_mat([c1, s1, zero,
-s1, c1, zero,
zero, zero, one])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = get_3x3_mat([one, s2, zero,
zero, c2, zero,
zero, zero, one])
 zoom_matrix = get_3x3_mat([one/height_zoom, zero, zero,
zero, one/width_zoom, zero,
zero, zero, one])
 shift_matrix = get_3x3_mat([one, zero, height_shift,
zero, one, width_shift,
zero, zero, one])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform(image, DIM=256):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = WSHIFT_ * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d,[DIM, DIM,3] )
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
def read_unlabeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['image_name']
def prepare_image(img, augment=True, tta=None, dim=256):
img = tf.image.decode_jpeg(img, channels=3)
 if dim==600:
img = tf.image.crop_to_bounding_box(img, 0, 100, 600, 600)
 else:
img = tf.image.crop_to_bounding_box(img, 83, 0, 500, 500)
 img = tf.image.resize(img, [dim,dim])
 img = tf.cast(img, tf.float32)/ 255.0
if tta!=None:
if tta[-3:]=='_lr':
img = tf.image.flip_left_right(img)
 tta = tta[:-3]
if tta=='rotate90':
img = tf.image.rot90(img,1)
 if tta=='rotate180':
img = tf.image.rot90(img,2)
 if tta=='rotate270':
img = tf.image.rot90(img,3)
 img = tf.reshape(img, [dim,dim, 3])
 return img
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1))
for filename in filenames]
return np.sum(n)
 count_data_items(files_test_600),count_data_items(files_test_500 )
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
def get_dataset(files, augment = False, tta=None, shuffle = False, repeat = False,
batch_size=16, dim=512):
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(1024*8)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 ds = ds.map(read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 ds = ds.map(lambda img, imgname_or_label:(prepare_image(img, augment=augment, tta=tta, dim=dim),
imgname_or_label),
num_parallel_calls=AUTO)
 ds = ds.batch(batch_size * REPLICAS)
 ds = ds.prefetch(AUTO)
 return ds
frames_per_video = 65
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )<define_variables>
EFNS = [efn.EfficientNetB0, efn.EfficientNetB1, efn.EfficientNetB2, efn.EfficientNetB3,
efn.EfficientNetB4, efn.EfficientNetB5, efn.EfficientNetB6, efn.EfficientNetB7]
def build_model(dim=128, ef=0):
inp = tf.keras.layers.Input(shape=(dim,dim,3))
if ef == RESNEXT_ID:
base = models.ResNeXt50(input_shape=(dim,dim,3),weights=None,include_top=False)
 else:
base = EFNS[ef](input_shape=(dim,dim,3),weights=None,include_top=False)
 x = base(inp)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x = tf.keras.layers.Dropout(0.5 )(x)
 x = tf.keras.layers.Dense(128,activation='relu' )(x)
 x = tf.keras.layers.Dropout(0.5 )(x)
 x = tf.keras.layers.Dense(5,activation='softmax' )(x)
 model = tf.keras.Model(inputs=inp,outputs=x)
 opt = tf.keras.optimizers.Adam(learning_rate=0.001)
 loss = tf.keras.losses.SparseCategoricalCrossentropy()
metrics = [tf.keras.metrics.SparseCategoricalAccuracy() ]
model.compile(optimizer=opt,loss=loss,metrics=[metrics])
 return model
build_model().summary()
input_size = 224<normalization>
def get_lr_callback(batch_size=8):
lr_start = 0.000005
lr_max = 0.00000125 * REPLICAS * batch_size
lr_min = 0.000001
lr_ramp_ep = 5
lr_sus_ep = 0
lr_decay = 0.8
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max - lr_start)/ lr_ramp_ep * epoch + lr_start
elif epoch < lr_ramp_ep + lr_sus_ep:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_ramp_ep - lr_sus_ep)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 return lr_callback
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<choose_model_class>
print('Getting test_ids')
 IMG_SIZE = 500
NUM_TEST_IMAGES = count_data_items(files_test_500)
 ds_test = get_dataset(files_test_500,augment=False,repeat=False,shuffle=False,
dim=IMG_SIZE,batch_size=BATCH_SIZE*4)
 test_ids_ds = ds_test.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )<load_pretrained>
tta_counter = 0
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint<predict_on_test>
VERBOSE = IS_INTERACTIVE
def generate_submission(EFF_NET,category,TTA):
if EFF_NET == RESNEXT_ID:
model_root = f'.. /input/cassava-category-{category}/ResNext50/ResNext50/'
else:
model_root = f'.. /input/cassava-category-{category}/B{EFF_NET}/B{EFF_NET}/'
if category%2==0:
IMG_SIZE = 600
files_test = files_test_600
elif category%2==1:
IMG_SIZE = 500
files_test = files_test_500
else:
assert 1==2, "INVALID CATEGORY"
global tta_counter
test_predictions = []
for fold in range(FOLDS):
print('
if EFF_NET == RESNEXT_ID:
print(' 
(IMG_SIZE,BATCH_SIZE*REPLICAS))
else:
print(' 
(IMG_SIZE,EFF_NET,BATCH_SIZE*REPLICAS))
K.clear_session()
with strategy.scope() :
model = build_model(dim=IMG_SIZE,ef=EFF_NET)
 print('Loading best model...')
 model.load_weights(model_root+'fold-%i.h5'%fold)
 if len(TTA)==0:
print('Predicting TEST without TTA...')
 ds_test = get_dataset(files_test,augment=False,repeat=False,shuffle=False,
dim=IMG_SIZE,batch_size=BATCH_SIZE)
 pred = model.predict(ds_test,verbose=VERBOSE,batch_size=BATCH_SIZE,
use_multiprocessing=True,workers=N_WORKERS)
 if 1:
print('Predicting TEST with TTA...')
 for x_ in range(1):
tta = TTA[tta_counter%8]
print(tta)
 tta_counter+=1
ds_test = get_dataset(files_test,augment=False,tta=tta,repeat=False,shuffle=False,
dim=IMG_SIZE,batch_size=BATCH_SIZE)
 if x_==0:
pred = model.predict(ds_test,verbose=VERBOSE,batch_size=BATCH_SIZE,
use_multiprocessing=True,workers=N_WORKERS)/len(TTA)
 else:
pred += model.predict(ds_test,verbose=VERBOSE,batch_size=BATCH_SIZE,
use_multiprocessing=True,workers=N_WORKERS)/len(TTA)
 test_pred_fold = pd.DataFrame(pred)
 test_pred_fold['fold'] = fold
test_pred_fold['image_id'] = test_ids
test_predictions.append(test_pred_fold)
 test_predictions = pd.concat(test_predictions)
 test_predictions_averaged = test_predictions.groupby('image_id')[[0,1,2,3,4]].mean()
test_predictions_averaged = test_predictions_averaged.sort_index()
return test_predictions_averaged
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5<predict_on_test>
CASSAVA_WEIGHTS = {(1, 'B0'): 0.0, 
(1, 'B1'): 0.0, 
(3, 'ResNext50'): 0.2144082584031884, 
(4, 'B0'): 0.04051596743735366, 
(4, 'B1'): 0.0059384033956569274, 
(4, 'B2'): 0.0, 
(4, 'B3'): 0.2772041683999385, 
(4, 'B4'): 1.0, 
(4, 'B5'): 1.0, 
(4, 'ResNext50'): 1.0, 
(5, 'B3'): 0.47634532056167095, 
(5, 'B4'): 0.0, 
(1, 'B2'): 0.0, 
(6, 'B4'): 0.81263538458694, 
(7, 'B1'): 0.6091246357587939, 
(7, 'B2'): 0.8727553512599798, 
(7, 'B3'): 1.0, 
(7, 'B4'): 0.0, 
(7, 'ResNext50'): 0.0, 
(8, 'B3'): 0.36729055080178946, 
(8, 'B4'): 0.3756019549511829, 
(8, 'ResNext50'): 1.0, 
(1, 'B3'): 0.0, 
(1, 'B4'): 1.0, 
(3, 'B0'): 0.0, 
(3, 'B1'): 0.0, 
(3, 'B2'): 0.0, 
(3, 'B3'): 0.3450946319830929, 
(3, 'B4'): 0.0}
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )<define_variables>
submission = pd.DataFrame(test_ids,columns=['image_id'])
 for x in range(5):
submission[x] = 0
submission = submission.set_index('image_id')
 submission = submission.sort_index()
submission
speed_test = False<predict_on_test>
import random
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )<predict_on_test>
submission.to_csv('predictions.csv')
 submission
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
K.clear_session()
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )<set_options>
device = cuda.get_current_device()
device.reset()
%reload_ext autoreload
%autoreload 2
%matplotlib inline 
<define_variables>
package_paths = [
'.. /input/pytorch-image-models/pytorch-image-models-master',
'.. /input/image-fmix/FMix-master'
]
for pth in package_paths:
sys.path.append(pth)
path = Path('data/MarchMadness')
 dest = path
dest.mkdir(parents=True, exist_ok=True)
 input_path = '.. /input/mens-machine-learning-competition-2019'
data_path = '.. /input/ncaa-19-dataprep/data/MarchMadness'<load_from_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 glob
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
df_test = pd.read_csv(f'{data_path}/df_test.csv', low_memory=False)
 df_msr = pd.read_csv(f'{data_path}/df_msr.csv', low_memory=False)
 df = pd.read_csv(f'{data_path}/df.csv', low_memory=False)
 sub = pd.read_csv(f'{input_path}/SampleSubmissionStage2.csv', low_memory=False)
 seeds = pd.read_csv(f'{input_path}/datafiles/NCAATourneySeeds.csv', low_memory=False)
 val_idxs = np.load(f'{data_path}/val_idxs.npy' )<set_options>
import os
import pandas as pd
import albumentations as albu
import matplotlib.pyplot as plt
import json
import seaborn as sns
import cv2
import albumentations as albu
import numpy as np
def random_seed(seed_value, use_cuda):
np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 random.seed(seed_value)
 if use_cuda:
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False 
<merge>
import torch
import torch.nn as nn
import torchvision.models as models
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torch.optim.lr_scheduler import ReduceLROnPlateau
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold, GroupKFold, KFold, train_test_split
from albumentations.pytorch import ToTensorV2
import time
import datetime
import copy
def join_df(left, right, left_on, right_on=None, on=None, how='left', suffix='_y'):
if right_on is None: right_on = left_on
return left.merge(right, left_on=left_on, right_on=right_on,
on=on, how=how, suffixes=("", suffix))<drop_column>
CFG = {
'fold_num': 10,
'seed': 719,
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 384,
'epochs': 32,
'train_bs': 32,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 4,
'used_epochs': [8],
'weights': [1,1,1,1,1]
}
base_cols = ['Score']
drop_cols = ['Loc', 'PointDiff_1', 'RankDiff_1', 'Seed_1', 'Seed_2']
for c in base_cols:
drop_cols.append(c+'_1')
 drop_cols.append(c+'_Opp_1')
 drop_cols.append(c+'_2')
 drop_cols.append(c+'_Opp_2')
 df.drop(drop_cols, axis=1, inplace=True)
 df_test.drop(drop_cols, axis=1, inplace=True)
<drop_column>
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
dep_var = 'result'
cat_vars = ['Season', 'TeamId_1', 'TeamId_2', 'Coach_1', 'Coach_2',
'Top5_1', 'Top5_2', 'Top25_1', 'Top25_2', 'Top50_1', 'Top50_2',
'ConfAbbrev_1', 'ConfAbbrev_2', 'Is_ConfGm', 'isMajor_1', 'isMajor_2']
cont_vars = [c for c in df.columns if c not in cat_vars]
cont_vars.remove('result')
 test = TabularList.from_df(df_test.copy() , path=path, cat_names=cat_vars, cont_names=cont_vars)
 procs=[FillMissing, Categorify, Normalize]<choose_model_class>
class CassavaDataset(Dataset):
def __init__(self,df:pd.DataFrame,imfolder:str,train:bool = True, transforms=None):
self.df=df
self.imfolder=imfolder
self.train=train
self.transforms=transforms
def __getitem__(self,index):
im_path=os.path.join(self.imfolder,self.df.iloc[index]['image_id'])
 x=cv2.imread(im_path,cv2.IMREAD_COLOR)
 x=cv2.cvtColor(x,cv2.COLOR_BGR2RGB)
 if(self.transforms):
x=self.transforms(image=x)['image']
if(self.train):
y=self.df.iloc[index]['label']
return x,y
else:
return x
def __len__(self):
return len(self.df )
learn = tabular_learner(data, layers=[200,100], emb_drop=0.2,
metrics=[accuracy])
 learn.model<train_model>
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 class CustomDeiT(nn.Module):
def __init__(self, model_name='model_name', pretrained=False):
super().__init__()
self.model = torch.hub.load('facebookresearch/deit:main', model_name, pretrained=0)
 n_features = self.model.head.in_features
self.model.head = nn.Linear(n_features, CFG.target_size)
 def forward(self, x):
x = self.model(x)
 return x
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 print(self.model)
 n_features = self.model.head.in_features
self.model.head = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x
class CustomViT(nn.Module):
def __init__(self, model_name='', pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=pretrained)
 n_features = self.model.head.in_features
self.model.head = nn.Linear(n_features,5)
 def forward(self, x):
x = self.model(x)
 return x
model = CustomViT(model_name='vit_base_patch16_384', pretrained=False ).to(device)
 device = torch.device(CFG['device'])
 model.to(device)
 model.eval()
learn.fit_one_cycle(2, wd=0.05 )<save_model>
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
learn.save('m_stage2_1')
<predict_on_test>
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,RandomCrop, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_inference_transforms(CFG):
return Compose([
RandomCrop(512,512),
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.)
preds, _ = learn.get_preds(DatasetType.Test)
 eps = 1e-5
df_test['Pred'] = np.clip(preds[:,1], eps, 1-eps)
 df_test = df_test[['Season', 'TeamId_1', 'TeamId_2', 'Pred']]
df_msr = df_msr[['Season', 'TeamId_1', 'TeamId_2', 'result']]
df_msr.reset_index(inplace=True, drop=True)
 df_m = join_df(df_msr, df_test, ['Season', 'TeamId_1', 'TeamId_2'])
 df_m = df_m[df_m.Pred.notnull() ]; df_m.head()
y_true = df_m.result
y_pred = df_m.Pred
log_loss(y_true, y_pred, eps=eps )<import_modules>
tst_preds = np.mean(tst_preds, axis=0)
import math
import numpy as np
import pandas as pd
from sklearn.metrics import log_loss
from sklearn.preprocessing import StandardScaler<merge>
test_pred = pd.DataFrame(tst_preds)
 test_pred['image_id'] = test.image_id
def Aggregate(teamcompactresults1,
teamcompactresults2,
merged_results,
regularseasoncompactresults):
winningteam1compactresults = pd.merge(how='left',
left=teamcompactresults1,
right=regularseasoncompactresults,
left_on=['year', 'team1'],
right_on=['Season', 'WTeamID'])
 winningteam1compactresults.drop(['Season',
'DayNum',
'WTeamID',
'LTeamID',
'LScore',
'WLoc',
'NumOT'],
inplace=True,
axis=1)
 grpwinningteam1resultsaverage = \
winningteam1compactresults.groupby(['year', 'team1'] ).mean()
winningteam1resultsaverage = grpwinningteam1resultsaverage.reset_index()
winningteam1resultsaverage.rename(columns={'WScore': 'team1WAverage'},
inplace=True)
 grpwinningteam1resultsmin = \
winningteam1compactresults.groupby(['year', 'team1'] ).min()
winningteam1resultsmin = grpwinningteam1resultsmin.reset_index()
winningteam1resultsmin.rename(columns={'WScore': 'team1Wmin'},
inplace=True)
 grpwinningteam1resultsmax = \
winningteam1compactresults.groupby(['year', 'team1'] ).max()
winningteam1resultsmax = grpwinningteam1resultsmax.reset_index()
winningteam1resultsmax.rename(columns={'WScore': 'team1Wmax'},
inplace=True)
 grpwinningteam1resultsmedian = \
winningteam1compactresults.groupby(['year', 'team1'] ).median()
winningteam1resultsmedian = grpwinningteam1resultsmedian.reset_index()
winningteam1resultsmedian.rename(columns={'WScore': 'team1Wmedian'},
inplace=True)
 grpwinningteam1resultsstd = \
winningteam1compactresults.groupby(['year', 'team1'] ).std()
winningteam1resultsstd = grpwinningteam1resultsstd.reset_index()
winningteam1resultsstd.rename(columns={'WScore': 'team1Wstd'},
inplace=True)
 losingteam1compactresults = pd.merge(how='left',
left=teamcompactresults1,
right=regularseasoncompactresults,
left_on=['year', 'team1'],
right_on=['Season', 'LTeamID'])
 losingteam1compactresults.drop(['Season',
'DayNum',
'WTeamID',
'LTeamID',
'WScore',
'WLoc',
'NumOT'],
inplace=True,
axis=1)
 grplosingteam1resultsaverage = \
losingteam1compactresults.groupby(['year', 'team1'] ).mean()
losingteam1resultsaverage = grplosingteam1resultsaverage.reset_index()
losingteam1resultsaverage.rename(columns={'LScore': 'team1LAverage'},
inplace=True)
 grplosingteam1resultsmin = \
losingteam1compactresults.groupby(['year', 'team1'] ).min()
losingteam1resultsmin = grplosingteam1resultsmin.reset_index()
losingteam1resultsmin.rename(columns={'LScore': 'team1Lmin'},
inplace=True)
 grplosingteam1resultsmax = \
losingteam1compactresults.groupby(['year', 'team1'] ).max()
losingteam1resultsmax = grplosingteam1resultsmax.reset_index()
losingteam1resultsmax.rename(columns={'LScore': 'team1Lmax'},
inplace=True)
 grplosingteam1resultsmedian = \
losingteam1compactresults.groupby(['year', 'team1'] ).median()
losingteam1resultsmedian = grplosingteam1resultsmedian.reset_index()
losingteam1resultsmedian.rename(columns={'LScore': 'team1Lmedian'},
inplace=True)
 grplosingteam1resultsstd = \
losingteam1compactresults.groupby(['year', 'team1'] ).std()
losingteam1resultsstd = grplosingteam1resultsstd.reset_index()
losingteam1resultsstd.rename(columns={'LScore': 'team1Lstd'},
inplace=True)
 winningteam2compactresults = pd.merge(how='left',
left=teamcompactresults2,
right=regularseasoncompactresults,
left_on=['year', 'team2'],
right_on=['Season', 'WTeamID'])
 winningteam2compactresults.drop(['Season',
'DayNum',
'WTeamID',
'LTeamID',
'LScore',
'WLoc',
'NumOT'],
inplace=True,
axis=1)
 grpwinningteam2resultsaverage = \
winningteam2compactresults.groupby(['year', 'team2'] ).mean()
winningteam2resultsaverage = grpwinningteam2resultsaverage.reset_index()
winningteam2resultsaverage.rename(columns={'WScore': 'team2WAverage'},
inplace=True)
 grpwinningteam2resultsmin = \
winningteam2compactresults.groupby(['year', 'team2'] ).min()
winningteam2resultsmin = grpwinningteam2resultsmin.reset_index()
winningteam2resultsmin.rename(columns={'WScore': 'team2Wmin'},
inplace=True)
 grpwinningteam2resultsmax = \
winningteam2compactresults.groupby(['year', 'team2'] ).max()
winningteam2resultsmax = grpwinningteam2resultsmax.reset_index()
winningteam2resultsmax.rename(columns={'WScore': 'team2Wmax'},
inplace=True)
 grpwinningteam2resultsmedian = \
winningteam2compactresults.groupby(['year', 'team2'] ).median()
winningteam2resultsmedian = grpwinningteam2resultsmedian.reset_index()
winningteam2resultsmedian.rename(columns={'WScore': 'team2Wmedian'},
inplace=True)
 grpwinningteam2resultsstd = \
winningteam2compactresults.groupby(['year', 'team2'] ).std()
winningteam2resultsstd = grpwinningteam2resultsstd.reset_index()
winningteam2resultsstd.rename(columns={'WScore': 'team2Wstd'},
inplace=True)
 losingteam2compactresults = pd.merge(how='left',
left=teamcompactresults2,
right=regularseasoncompactresults,
left_on=['year', 'team2'],
right_on=['Season', 'LTeamID'])
 losingteam2compactresults.drop(['Season',
'DayNum',
'WTeamID',
'LTeamID',
'WScore',
'WLoc',
'NumOT'],
inplace=True,
axis=1)
 grplosingteam2resultsaverage = \
losingteam2compactresults.groupby(['year', 'team2'] ).mean()
losingteam2resultsaverage = grplosingteam2resultsaverage.reset_index()
losingteam2resultsaverage.rename(columns={'LScore': 'team2LAverage'},
inplace=True)
 grplosingteam2resultsmin = \
losingteam2compactresults.groupby(['year', 'team2'] ).min()
losingteam2resultsmin = grplosingteam2resultsmin.reset_index()
losingteam2resultsmin.rename(columns={'LScore': 'team2Lmin'},
inplace=True)
 grplosingteam2resultsmax = \
losingteam2compactresults.groupby(['year', 'team2'] ).max()
losingteam2resultsmax = grplosingteam2resultsmax.reset_index()
losingteam2resultsmax.rename(columns={'LScore': 'team2Lmax'},
inplace=True)
 grplosingteam2resultsmedian = \
losingteam2compactresults.groupby(['year', 'team2'] ).median()
losingteam2resultsmedian = grplosingteam2resultsmedian.reset_index()
losingteam2resultsmedian.rename(columns={'LScore': 'team2Lmedian'},
inplace=True)
 grplosingteam2resultsstd = \
losingteam2compactresults.groupby(['year', 'team2'] ).std()
losingteam2resultsstd = grplosingteam2resultsstd.reset_index()
losingteam2resultsstd.rename(columns={'LScore': 'team2Lstd'},
inplace=True)
 agg_results = pd.merge(how='left',
left=merged_results,
right=winningteam1resultsaverage,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsaverage,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam1resultsmin,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsmin,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam1resultsmax,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsmax,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam1resultsmedian,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsmedian,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam1resultsstd,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsstd,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsaverage,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsaverage,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsmin,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsmin,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsmax,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsmax,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsmedian,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsmedian,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsstd,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsstd,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 return agg_results 
<load_from_csv>
test_pred = test_pred.set_index('image_id' )
def GrabData() :
tourneyresults = pd.read_csv('.. /input/stage2datafiles/NCAATourneyCompactResults.csv')
 tourneyseeds = pd.read_csv('.. /input/stage2datafiles/NCAATourneySeeds.csv')
 regularseasoncompactresults = \
pd.read_csv('.. /input/stage2datafiles/RegularSeasonCompactResults.csv')
 sample = pd.read_csv('.. /input/SampleSubmissionStage2.csv')
 results = pd.DataFrame()
results['year'] = tourneyresults.Season
results['team1'] = np.minimum(tourneyresults.WTeamID, tourneyresults.LTeamID)
 results['team2'] = np.maximum(tourneyresults.WTeamID, tourneyresults.LTeamID)
 results['result'] =(tourneyresults.WTeamID <
tourneyresults.LTeamID ).astype(int)
 merged_results = pd.merge(left=results,
right=tourneyseeds,
left_on=['year', 'team1'],
right_on=['Season', 'TeamID'])
 merged_results.drop(['Season', 'TeamID'], inplace=True, axis=1)
 merged_results.rename(columns={'Seed': 'team1Seed'}, inplace=True)
 merged_results = pd.merge(left=merged_results,
right=tourneyseeds,
left_on=['year', 'team2'],
right_on=['Season', 'TeamID'])
 merged_results.drop(['Season', 'TeamID'], inplace=True, axis=1)
 merged_results.rename(columns={'Seed': 'team2Seed'}, inplace=True)
 merged_results['team1Seed'] = \
merged_results['team1Seed'].apply(lambda x: str(x)[1:3])
 merged_results['team2Seed'] = \
merged_results['team2Seed'].apply(lambda x: str(x)[1:3])
 merged_results = merged_results.astype(int)
 winsbyyear = regularseasoncompactresults[['Season', 'WTeamID']].copy()
winsbyyear['wins'] = 1
wins = winsbyyear.groupby(['Season', 'WTeamID'] ).sum()
wins = wins.reset_index()
lossesbyyear = regularseasoncompactresults[['Season', 'LTeamID']].copy()
lossesbyyear['losses'] = 1
losses = lossesbyyear.groupby(['Season', 'LTeamID'] ).sum()
losses = losses.reset_index()
winsteam1 = wins.copy()
winsteam1.rename(columns={'Season': 'year',
'WTeamID': 'team1',
'wins': 'team1wins'}, inplace=True)
 winsteam2 = wins.copy()
winsteam2.rename(columns={'Season': 'year',
'WTeamID': 'team2',
'wins': 'team2wins'}, inplace=True)
 lossesteam1 = losses.copy()
lossesteam1.rename(columns={'Season': 'year',
'LTeamID': 'team1',
'losses': 'team1losses'}, inplace=True)
 lossesteam2 = losses.copy()
lossesteam2.rename(columns={'Season': 'year',
'LTeamID': 'team2',
'losses': 'team2losses'}, inplace=True)
 merged_results = pd.merge(how='left',
left=merged_results,
right=winsteam1,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=lossesteam1,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=winsteam2,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=lossesteam2,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 teamcompactresults1 = merged_results[['year', 'team1']].copy()
teamcompactresults2 = merged_results[['year', 'team2']].copy()
train = Aggregate(teamcompactresults1,
teamcompactresults2,
merged_results,
regularseasoncompactresults)
 sample['year'] = sample.ID.apply(lambda x: str(x)[:4] ).astype(int)
 sample['team1'] = sample.ID.apply(lambda x: str(x)[5:9] ).astype(int)
 sample['team2'] = sample.ID.apply(lambda x: str(x)[10:14] ).astype(int)
 merged_results = pd.merge(how='left',
left=sample,
right=tourneyseeds,
left_on=['year', 'team1'],
right_on=['Season', 'TeamID'])
 merged_results.drop(['Season', 'TeamID'], inplace=True, axis=1)
 merged_results.rename(columns={'Seed': 'team1Seed'}, inplace=True)
 merged_results = pd.merge(how='left',
left=merged_results,
right=tourneyseeds,
left_on=['year', 'team2'],
right_on=['Season', 'TeamID'])
 merged_results.drop(['Season', 'TeamID'], inplace=True, axis=1)
 merged_results.rename(columns={'Seed': 'team2Seed'}, inplace=True)
 merged_results['team1Seed'] = \
merged_results['team1Seed'].apply(lambda x: str(x)[1:3] ).astype(int)
 merged_results['team2Seed'] = \
merged_results['team2Seed'].apply(lambda x: str(x)[1:3] ).astype(int)
 merged_results = pd.merge(how='left',
left=merged_results,
right=winsteam1,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=lossesteam1,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=winsteam2,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=lossesteam2,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 teamcompactresults1 = merged_results[['year', 'team1']].copy()
teamcompactresults2 = merged_results[['year', 'team2']].copy()
test = Aggregate(teamcompactresults1,
teamcompactresults2,
merged_results,
regularseasoncompactresults)
 return train, test<drop_column>
test_pred.to_csv('vit_predictions.csv' )
train, test = GrabData()
trainlabels = train.result.values
train.drop('result', inplace=True, axis=1)
 train.fillna(-1, inplace=True)
 testids = test.ID.values
test.drop(['ID', 'Pred'], inplace=True, axis=1)
 test.fillna(-1, inplace=True )<normalization>
ss = StandardScaler()
train[train.columns[3:]] = np.round(ss.fit_transform(train[train.columns[3:]]), 6)
 train['target'] = trainlabels 
<save_to_csv>
train[train.columns[3:]].to_csv('mensdata.csv',index=False )<compute_test_metric>
def Outputs(data):
return 1./(1.+np.exp(-data))
def GPIndividual1(data):
predictions =(1.0*np.tanh(((((((((((( data["team2Seed"])+(data["team1Lmin"])) /2.0)) +(data["team2Seed"])) /2.0)) +(data["team2Seed"])) /2.0)) -(data["team1Seed"])))+
1.0*np.tanh(((((((((( data["team1Lmin"])/ 2.0)) +(data["team2Seed"])) /2.0)) *(((data["team2Seed"])*(((data["team2Seed"])-(data["team1Seed"])))))))/ 2.0)) +
0.943544*np.tanh(((((((( np.where(data["team1wins"]>0,(( data["team1losses"])/ 2.0),(( data["team1Lmin"])-(data["team1WAverage"])))) +(data["team1wins"])) /2.0)) / 2.0)) / 2.0)))
 return Outputs(predictions)
 def GPIndividual2(data):
predictions =(1.0*np.tanh(((((( data["team2Seed"])+(((((((data["team1Lmin"])+(data["team2Seed"])) /2.0)) +(data["team2Seed"])) /2.0)))/2.0)) -(data["team1Seed"])))+
1.0*np.tanh(((((((data["team1Seed"])*(data["team1Seed"])))-(data["team1Seed"])))*(((((((data["team2Seed"])-(data["team1Seed"])))/ 2.0)) / 2.0)))) +
0.942762*np.tanh(((((((((data["team2Seed"])/ 2.0)) *(data["team1Seed"])))*(((( data["team2wins"])+(data["team1Seed"])) /2.0)))) / 2.0)))
 return Outputs(predictions)
 def GPIndividual3(data):
predictions =(1.0*np.tanh(((((( data["team2Seed"])+(((((((data["team2Seed"])+(data["team1Lmin"])) /2.0)) +(data["team2Seed"])) /2.0)))/2.0)) -(data["team1Seed"])))+
1.0*np.tanh(((((((((data["team2Seed"])-(data["team1Seed"])))*(((( data["team1losses"])+(((data["team1Seed"])/ 2.0)))/2.0)))) *(data["team1Seed"])))/ 2.0)) +
1.0*np.tanh(((((((data["team1Seed"])-(data["team2Seed"])))*(((((((data["team2Seed"])/ 2.0)) / 2.0)) *(data["team1Seed"])))))/ 2.0)))
 return Outputs(predictions)
 def GP(data):
return(GPIndividual1(data)+GPIndividual2(data)+GPIndividual3(data)) /3 .<compute_test_metric>
print(log_loss(train.target,GPIndividual1(train)))
 print(log_loss(train.target,GPIndividual2(train)))
 print(log_loss(train.target,GPIndividual3(train)))
 print(log_loss(train.target,GP(train)) )<save_to_csv>
pred1 = pd.read_csv('./predictions.csv',index_col=0 ).sort_index()
pred3 = pd.read_csv('./vit_predictions.csv',index_col=0 ).sort_index()
pred1 = pred1.div(pred1.sum(axis=1),axis=0)
 pred3 = pred3.div(pred3.sum(axis=1),axis=0 )
test[test.columns[3:]] = np.round(ss.transform(test[test.columns[3:]]), 6)
 predictions = GP(test)
 submission = pd.DataFrame({'ID': testids,
'Pred': np.clip(predictions.values,.01,.99)})
 submission.to_csv('submission.csv', index=False )<load_from_csv>
submission = 0.8*pred1 + 0.2*pred3
data_dir = '.. /input/'
df_seeds = pd.read_csv(data_dir + 'stage2datafiles/NCAATourneySeeds.csv')
 df_tour = pd.read_csv(data_dir + 'stage2datafiles/NCAATourneyCompactResults.csv')
 df_massey = pd.read_csv(data_dir + 'masseyordinals/MasseyOrdinals.csv' )<data_type_conversions>
submission['label'] = submission.idxmax(axis=1)
 submission = submission.reset_index()
submission
<groupby><EOS> 
submission[['image_id','label']].to_csv('submission.csv',index=False )