Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<predict_on_test>
PATH = '/kaggle/input/tabular-playground-series-jan-2021/'
for idx,row in tqdm(df_sample.iterrows() ,total=len(df_sample)) :
idx = row['id']
print("Computing predictions for image", idx)
 rle = make_predictions(idx)
 names.append(idx)
 preds.append(rle )<save_to_csv>
train = pd.read_csv(PATH+'train.csv')
 test = pd.read_csv(PATH+'test.csv')
 submission = pd.read_csv(PATH+'sample_submission.csv' )
df = pd.DataFrame({'id': names, 'predicted': preds})
 df.to_csv('submission.csv',index=False )<install_modules>
!pip install pycaret
!pip install --no-index --find-links=.. /input/preinstall efficientnet<import_modules>
from pycaret.regression import *
import numpy as np
import pandas as pd
import os
import glob
import gc
from functools import partial
import json
import rasterio
from rasterio.windows import Window
import yaml
import pprint
import pathlib
from tqdm.notebook import tqdm
import cv2
import tensorflow as tf
import efficientnet as efn
import efficientnet.tfkeras
import time<define_variables>
reg = setup(data=train, target='target', silent=True, session_id=2021 )
mod_paths = ['.. /input/hubmap-ensamble-model1/','.. /input/hubmap-ensamble-model2/']
THRESHOLD = 0.5
BATCH_SIZE = 256
CHECKSUM = False<load_pretrained>
blended = blend_models(best_3, fold=5 )
identity = rasterio.Affine(1, 0, 0, 0, 1, 0)
 fold_models = []
for mod_path in mod_paths:
with open(mod_path+'params.yaml')as file:
P = yaml.load(file, Loader=yaml.FullLoader)
 pprint.pprint(P)
 with open(mod_path + 'metrics.json')as json_file:
M = json.load(json_file)
 print('Model run datetime: '+M['datetime'])
 print('OOF val_dice_coef: ' + str(M['oof_dice_coef']))
for fold_model_path in glob.glob(mod_path+'*.h5'):
fold_models.append(tf.keras.models.load_model(fold_model_path,compile = False))
print(len(fold_models))<define_variables>
pred_holdout = predict_model(blended )
WINDOW = P['TILE'] if 'TILE' in P.keys() else P['DIM_FROM']
CROP_SIZE = WINDOW//2
INPUT_SIZE = P['INPUT_SIZE']<define_variables>
final_model = finalize_model(blended )
MIN_OVERLAP = WINDOW - CROP_SIZE
BOARD_CUT =(WINDOW - CROP_SIZE)//2<prepare_x_and_y>
predictions = predict_model(final_model, data=test )
def rle_encode_less_memory(img):
pixels = np.concatenate([[False], img.T.flatten() , [False]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 def make_grid(shape, window, min_overlap=0, board_cut = 0):

 step = window - min_overlap
x, y = shape
start_x = np.arange(0, max(x-window+board_cut,1), step, dtype=np.int64)
 start_y = np.arange(0, max(y-window+board_cut,1), step, dtype=np.int64)
 if start_x[-1] < x-window+board_cut and x-window+board_cut > 0:
start_x = np.concatenate([start_x,[x-window+board_cut]])
 if start_y[-1] < y-window+board_cut and y-window+board_cut > 0:
start_y = np.concatenate([start_y,[y-window+board_cut]])
 slices = np.zeros(( len(start_x),len(start_y), 4), dtype=np.int64)
 for i in range(len(start_x)) :
for j in range(len(start_y)) :
slices[i,j] = start_x[i], min(start_x[i]+window,x), start_y[j], min(start_y[j]+window,y)
 return slices.reshape(len(start_x)*len(start_y),4 )<prepare_x_and_y>
submission['target'] = predictions['Label']
AUTO = tf.data.experimental.AUTOTUNE
image_feature = {
'image': tf.io.FixedLenFeature([], tf.string),
'x1': tf.io.FixedLenFeature([], tf.int64),
'y1': tf.io.FixedLenFeature([], tf.int64)
 }
def _parse_image(example_proto):
example = tf.io.parse_single_example(example_proto, image_feature)
 image = tf.reshape(tf.io.decode_raw(example['image'],out_type=np.dtype('uint8')) ,(P['DIM'],P['DIM'], 3))
return image, example['x1'], example['y1']
def load_dataset(filenames, ordered=True):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(_parse_image)
 return dataset
def get_dataset(FILENAME):
dataset = load_dataset(FILENAME)
 dataset = dataset.batch(64)
 dataset = dataset.prefetch(AUTO)
 return dataset<save_to_csv>
submission.to_csv('submission_0116_baseline.csv', index=False )